I'm trying to remove first word from given string. I'm done so far...
$word = 'removeMe|meow|whatever';
$needle = 'removeMe';
$haystack = ''; // To replace with.
$word = str_replace( $needle, $haystack, $word );
It's working great, but problem is when $word is something like this...
$word = 'removeMe|meow|removeMe|whatever';
I don't want to remove second $needle. Is it possible and how? )
Try this :
Unfortunately PHP doesn't support this by default. Here's a function doing it:
If you want to replace the text only at the beginning of the string:
If your string is actually a
|
-separated list, see @lonesomeday's answer.I would do something like this:
PHPs preg_replace supports this directly through the limit parameter
like:
If your needle only appears at the beginning, sth. like a simple
should suffice.
Regards
rbo
I was gonna go with an explode, implode, but this is better and shorter. It will leave the first pipe symbol though.
echo $string = strstr($string , '|');
If your string is space separated, just replace the pipe with a space
Third parameter of the explode function limits how many times the word will be split. This allowed a very clean solution for me. From php.net: