I have a string which I want to search through for a given expression. If I find this expression I want to insert a new string behind it. How do I do this?
| SitePoint Sponsor |

I have a string which I want to search through for a given expression. If I find this expression I want to insert a new string behind it. How do I do this?



Posting a sample would have deffinatly helped in explaning exactly what you are trying to do.
I interpreted it as having a string like "blah something blah" and then you want to have PHP search the string for "something" and if it finds it, add something to the string (like " else", for example) after where "something" was found.
If that is the case, you can do it like this:
That would echo "blah something else blah". You can change the first two arguments to str_replace() as needed. Is that what you want?PHP Code:$text = 'blah something blah';
echo str_replace('something', 'something else', $text);

ok heres what I have in mind:
$string = "blah something blah";
$expression = "something";
$insertvalue = "else";
search through $string for $expression;
if($string contains $expression)
{
insert $insertvalue into $string ->behind<- $expression;
}



Ok. The code I posted should do almost exactly that.
Use:
Let me know if you need any more help.PHP Code:$text = str_replace('something', 'something else', $text);

Only problem is that in your code is that the expression you search for get replaced. Say that the expression is 1000 characters long, it would'nt be very efficient. I need to insert 'else' , and only that, behind the expression searhed for.

ok I've altered my code a bit so it works with str_replace. Only problem now is that str_replace replaces all occurrences of the search string with the replacement string. Is there a way to get it to only replace the first occurence of the search string?

I tried this:
preg_replace($expression,$expression.$insertvalue,$string,1);
always get errormessage saying that delimiter must be alphanumeric???????????
Try this instead:
your regex must be properly delimited by non-alphanumeric characters (i used backslashes above)Code:<?php $search = 'test'; $string = "this is a test. we'll have another test tommorrow."; $add = ' really!'; $string = preg_replace("/(.+)$search/U", "$1 $search"."$add", $string, 1); echo $string; ?>

thanx!!! it works perfectly
I dont understand anything of the code, but as long as it works i'm happy![]()
![]()
![]()
![]()
no problemCode:$string = preg_replace("/(.+)$search/U", "$1 $search"."$add", $string, 1);
that's the crux up above... the 1st parameter is the regex which is delimited by the slashes '/'... the parentheses capture the parts of the string that match '.+', which basically means '1 or more of any character'... we want to follow that up with $search so that we know where we can add the string after... the 'U' behind the modifiers makes the regex 'ungreedy' so that it doesn't eat up $search terms in the string if there are more than 1 occurence (since we only want to replace the one after the 1st occurence of $search)...
Bookmarks