user4fun Posted August 15, 2007 Share Posted August 15, 2007 $firstvar = "aaa&SomeMoreStuff"$newvar = i want to replace every thing from start up to and including the {&} with FOUND.$newvar should becomeFOUNDSomeMoreStuffsome $firstvar would have more than aaait could be anything elsehere is what i have $firstvar = "aaa&SomeMoreStuff";$newvar = str_replace("&","FOUND",$firstvar);echo $newvar; result is aaaFOUNDSomeMoreStuffneed to get rid of every thing before FOUND Link to comment Share on other sites More sharing options...
boen_robot Posted August 15, 2007 Share Posted August 15, 2007 There's no way to do that with str_replace(). You'll need to use regular expressions with ereg_replace() or something like it.I'm not sure, but I think this might do what you want: $firstvar = "aaa&SomeMoreStuff";$newvar = ereg_replace('^.*\&','FOUND',$firstvar);echo $newvar; Search Google for "regex tutorial" or "regular expressions" to find more about regular expressions, which is the first argument of this function. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 15, 2007 Share Posted August 15, 2007 Otherwise you would need to use strpos to find the position of the &, and then use substr to get everything after it. You can add the FOUND text onto the beginning yourself. $pos = strpos($str, "&");if ($pos !== false){ $str = "FOUND" . substr($str, $pos);} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.