Forum Moderators: coopster
for example, if i want to match both "Hello World" and "Hello \n World" (\n being line break), then i can do:
preg_replace("/Hello [\n]{0,1}World/","change it to this",$input);
as i suspect you want to support a linebreak in any random place, then i would first backup the string...
$temp=$input;
preg_match("/$search/",$input);
preg_replace("/$search/",$change_to,$temp);
if(strpos("\n",$temp)) //ie, only if there is one to change
{
$test=preg_replace("/\n/","",$temp,1); //ie, remove the first linebreak to be found in $temp
}
if ($strpos=strpos($test,$test))
{
//now we replace in the "master string" $temp this place
$temp=substr_replace($temp,$change_to,$strpos,strlen($search)+strlen("\n"));
//ie, we find that part of $temp which corresponds to the matched string in $test and replace it
}
$temp=preg_replace("/\n/","\a",$temp,1);
<?php
$input="Mrs Smith's cat\n went splat cat \nwent splat.";
$search="cat went";
$change_to="dog said";
$temp=preg_replace("/$search/",$change_to,$input);
while(preg_match("/\n/",$temp)) //ie, only if there is one to change
{
$test=preg_replace("/\n/","",$temp,1); //ie, remove the first linebreak to be found in $temp
if ($strpos=strpos($test,$search)) //ie, if the search can be found now
{
//now we replace in the "master string" $temp this place
$temp=substr_replace($temp,$change_to,$strpos,strlen($search)+strlen("\n"));
//ie, we find that part of $temp which corresponds to the matched string in $test and replace it
}
$temp=preg_replace("/\n/","\a",$temp,0);
}
$output=str_replace("\a","\n",$temp);
echo $output;
?>