Preg_replace question

Advert test
#1
I have the below line working fine, it only replaces the text if host is assign nothing:
$content = str_replace("host = ''", "host = 'test1'", $content);

I'm trying to use preg_replace to always replace the host value even if its not null with something like:
$content = preg_replace ('/\host\=\'(.*?)\](.*?)\'/is', 'host\=\'test1\'', $content);
 
#2
hi so what is the pattern you are looking for?

In your str_replace example the pattern is: host[SPACE]=[SPACE]''

but in your preg_replace example you have, the pattern is: host='' (notice the spaces)

If you can give a snippet of the content and the exact pattern you want to replace I can point you in the right direction
 
#3
Sorry about that, the pattern is host[space]=[space]''
Code:
host = ''
Also is it possible to search for say $host = '' without the preg_replace assuming the text $host is a variable as this was causing problems with the string replace hence why im using just host = ''?
 
#4
<?php

$content1 = "host = 'foobarbaz'";
$content2 = "host = ''";

$content1 = preg_replace ("/host = \'(.*)\'/is", "host = 'test1'", $content1);
$content2 = preg_replace ("/host = \'(.*)\'/is", "host = 'test1'", $content2);

echo $content1 . '<br />';
echo $content2 . '<br />';

?>

sorry not sure what you mean by the last question but if you enapsulate $host in singlequotes it will be treated as a literal string. If you put encapsulate it in doublequotes you will have to escape the $, is this what you mean?
 
#5
Having problems with the below? Any ideas?
$content = preg_replace("/\$host = \'(.*)\'\;/is", "\$host = 'testing';", $content);

The pattern is $host = '';
 
#6
try this?

<?php

$content = "$host = '';";

$content = preg_replace("/$host = \'(.*)\';/is", "\$host = 'testing';", $content);

echo $content

?>
 
#9
Hi,

Can you post the entire content of the $content variable you are trying to replace, rather than just the pattern you are looking to replace.

If it is sensitive in nature, please pm it to me.

I'll be able to sort it out for you then.

Thanks
 
#10
<?php
#####################
#####################

//WARNING: DON'T CHANGE BELOW HERE
//Database details
$host = '';
$username = '';
$pass = '';
$prefix ='';

function checkInstallation() {
global $host;
$validate = 0;

if($host == '')
$validate = 1;

return $validate;
}
?>

N.B. - When I post the code using PHP,CODE or QUOTE tags it removes all the variables, i think its due to the site not liking the dollar symbol.
 
#13
After slight twist to the problem. It will change one line no problem but if i have more then one preg_replace it messes up the whole file.

I have these preg_replaces:
<?php
$content = preg_replace("#DBHost\s=\s([^s]*)?#is", "DBHost = '".$DBHost."'", $content);
//$content = preg_replace("#DBName\s=\s([^s]*)?#is", "DBName = '".$DBName."'", $content);
//$content = preg_replace("#DBUser\s=\s([^s]*)?#is", "DBUser = '".$DBUser."'", $content);
//$content = preg_replace("#DBPass\s=\s([^s]*)?#is", "DBPass = '".$DBPass."'", $content);
//$content = preg_replace("#DBPrefix\s=\s([^s]*)?#is", "DBPrefix = '".$DBPrefix."'", $content);
?>

And they are changing the below file:
<?php
$DBHost = '';
$DBName = '';
$DBUser = '';
$DBPass = '';
$DBPrefix = '';
?>

But currently its changing it to:
<?php
$DBHost = 'localhost';ser = '';
$DBPass = '';
$DBPrefix = '';
?>
 
#15
function setDBDetails($DBHost,$DBName,$DBUser,$DBPass,$DBPrefix) {
global $configFileLoc;

$fhandle = fopen($configFileLoc,"r");
$content = fread($fhandle,filesize($configFileLoc));

$content = preg_replace("#DBHost\s=\s([^s]*)?#is", "DBHost = '".$DBHost."'", $content);
//$content = preg_replace("#DBName\s=\s([^s]*)?#is", "DBName = '".$DBName."'", $content);
//$content = preg_replace("#DBUser\s=\s([^s]*)?#is", "DBUser = '".$DBUser."'", $content);
//$content = preg_replace("#DBPass\s=\s([^s]*)?#is", "DBPass = '".$DBPass."'", $content);
//$content = preg_replace("#DBPrefix\s=\s([^s]*)?#is", "DBPrefix = '".$DBPrefix."'", $content);

$fhandle = fopen($configFileLoc,"w");
fwrite($fhandle,$content);
fclose($fhandle);
}
 
#16
By the way, have a look at the parameters you're passing to your function...

function setDBDetails($DBHost,$DBName,$DBUser,$DBPass,$DBPr efix) {

There's a space between $DBPr and efix!
 
#17
Hi, try this, modified Louie's code to add multi-line and dynamic text capabilities...

<?php

function setDBDetails($DBHost,$DBName,$DBUser,$DBPass,$DBPrefix) {

global $configFileLoc;

$fhandle = fopen($configFileLoc,"r");
$content = fread($fhandle,filesize($configFileLoc));

$content = preg_replace("#([a-zA-Z]+)\s=\s([^s]*)?#isU", "$1 = '\$" . $1 . "'", $content);

$fhandle = fopen($configFileLoc,"w");
fwrite($fhandle,$content);
fclose($fhandle);

}

?>
 
#18
By the way, have a look at the parameters you're passing to your function...

function setDBDetails($DBHost,$DBName,$DBUser,$DBPass,$DBPr efix) {

There's a space between $DBPr and efix!

This is ok, its just the way it copied and pasted, thanks for the heads up.



Hi, try this, modified Louie's code to add multi-line and dynamic text capabilities...

<?php

function setDBDetails($DBHost,$DBName,$DBUser,$DBPass,$DBPrefix) {

global $configFileLoc;

$fhandle = fopen($configFileLoc,"r");
$content = fread($fhandle,filesize($configFileLoc));

$content = preg_replace("#([a-zA-Z]+)\s=\s([^s]*)?#isU", "$1 = '\$" . $1 . "'", $content);

$fhandle = fopen($configFileLoc,"w");
fwrite($fhandle,$content);
fclose($fhandle);

}

?>

I can't make out what the preg_replace is doning, can you explain it please?
 
#19
Hi,

([a-zA-Z]+) - where this is located it will match all thr strings DBHost, DBName, DBPass etc and assign them to a variable called $1

isU - each of these letters are called modifiers, "i" will set any matches to case insensitive, letters can be uppercase or lowercase. "s" can have various uses but I tend to use it to include newlines in any patterns. "U" will set the greediness of the match, it can be used in numerous ways but here I use it to be 'greedy' and match all instances of the pattern that it finds in the entire file.

"$1 = '\$" . $1 . "'" - when matched, this will translate to something like "DBHost = $DBHost" ($DBHost should be parsed by the code to show whatever the value of the $DBHost parameter is) the "$1" variable will contain the matched text within the first set of parenthesis () in the pattern

All in all, what the code should do is to go through your file and replace all occurences of the pattern you are looking to replace, no need for multiple preg_replace patterns. I'm sure Louie would agree, if we had every bit of this code open in front of us in our code editor, we'd have it sorted out in a jiffy. All this post tabletennis is pretty confusing!

Have you tried it yet?
 
#20
As said we need to have a go at this thing from our point of view (e.g. like the content in the file that requires the changes).
 
Top