how to replace string on first occurrence only

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crishna369
Forum Newbie
Posts: 10
Joined: Mon May 25, 2009 12:35 am

how to replace string on first occurrence only

Post by crishna369 »

I wanna know how i can replace a particular string only on first occurrence.
somebody plz reply.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: how to replace string on first occurrence only

Post by mikemike »

Try this:

Code: Select all

<?php
$var = 'weeeeeeeee weeeeeeeee abc 123 lets all do the abc, its easy as 123';
// pattern, replacement, string, limit
echo preg_replace('/abc/', '123', $var, 1); 
?>
Remember that you should only use preg_replace with reg exp as it's slower than normal replace functions.
Last edited by Benjamin on Tue May 26, 2009 10:57 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: how to replace string on first occurrence only

Post by requinix »

If you want the (probably) faster string-based method, use strpos to find the first occurrence, strlen to know how much to remove, and substr_replace to do the replacement.


(Yeah, I know, not going to happen. mike posted code, I posted words. If someone is typing "plz" then there's no way they'll take the harder of the suggestions, regardless of any reasons why they should.)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: how to replace string on first occurrence only

Post by mikemike »

hehe :wink:
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: how to replace string on first occurrence only

Post by mikemike »

I'm not convinced your method would be faster you know. I did some benchmarking on the regexp engine and normal replacement not so long ago and it's not that much slower.
crishna369
Forum Newbie
Posts: 10
Joined: Mon May 25, 2009 12:35 am

Re: how to replace string on first occurrence only

Post by crishna369 »

below is my code:

Code: Select all

$val = "(PT) :- PTFE with 316L SST(Diaphragm) 316L SST(Others)";
    $t = preg_replace('/(/', '', $val, 1);
    $tp = preg_replace('/)/', '', $t, 1);
    $temp = str_replace($ans['Code'],"",$tp);
    $temp1 = str_replace(":-","",$temp);
    echo $temp1;
And I m getting this error:
Warning: preg_replace() [function.preg-replace]: Compilation failed: missing ) at offset 1 in C:\xampp\htdocs\crm\replace.php on line 11

Warning: preg_replace() [function.preg-replace]: Compilation failed: unmatched parentheses at offset 0 in C:\xampp\htdocs\crm\replace.php on line 12
():-
Last edited by Benjamin on Tue May 26, 2009 10:58 am, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: how to replace string on first occurrence only

Post by prometheuzz »

mikemike wrote:I'm not convinced your method would be faster you know. I did some benchmarking on the regexp engine and normal replacement not so long ago and it's not that much slower.
On fairly small strings, it doesn't make an awful lot of difference. At least, not noticeable by humans. But, if you're doing a "proper" test on large strings and repeating the tests a number of thousands of times, then you will notice that preg_replace will be much slower than a "simple" string-replace function.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: how to replace string on first occurrence only

Post by mikemike »

I appreciate that, but you're not running one function, you're running 3, all for the same result.
Post Reply