Re: ereg_replace: Replacing only first occurrence
| From: | ~~~i LeoNid ~~ | Date: | Mon, 23 Apr 2001 22:15:01 +0000 |
| Subject: | Re: ereg_replace: Replacing only first occurrence | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-49955@lists.php.net to get a copy of this message | ||
On 23 Apr 2001 13:04:36 -0700 impersonator of
corky6921@nospamplease-yahoo.com ("Erica Douglass") planted &I saw in
php.general:
>I want to only replace the first occurrence of a string in a file using
>ereg_replace. Should I use a loop to do this? Any suggestions? Please email
>me at corky6921@yahoo.com with suggestions.
>
>Thanks,
>Erica
>
I think, begining from some php >4, preg_replace allows to do this,
Otherwise, you could use preg_match to find first match then strpos() to
find it position, and, finaly substr() with concatenation to your
replacement. This loong way was the first, that came to my mind for cases,
where regexp realy needed. (If they are not needed, str_replace() would
work faster/simlier, you know o'cos:).
Note: for some strings I experience hangup, especially w/ereg_replace,
but also, for long ones, w/preg0. Regards. Leonid.