Coolkill
asked on
Function to replace first occurence only.
Hi,
I'm after a function to replace the first occurance of string only, with a replacement string..
basically ,str_replace but only for the first occurance, not all..
Anyone up for it?
I'm after a function to replace the first occurance of string only, with a replacement string..
basically ,str_replace but only for the first occurance, not all..
Anyone up for it?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Roonaan is not correct.
The 4th parameter is used to return the number of times it was replaced, not to control how many times it was replaced. Notice the & in the function definition. This is an output property, not an input property. Though I DO think it should act as both!
Proof.
<?php
$count = 2;
$str = str_replace("ll", "", "good golly miss molly dolly!", $count);
echo $count . ' ' . $str;
?>
Roonaan would be expecting output of ...
2 good goy miss moy dolly!
The actual output is ...
3 good goy miss moy doy!
Trying to run the code with a hard-coded value in the place of $count results in ...
Fatal error: Only variables can be passed by reference in C:\d6.php on line 3
To only replace the first occurance of a substring then use the preg_replace() function as described by kebabs.
http://www.php.net/manual/en/function.preg-replace.php
The 4th parameter is used to return the number of times it was replaced, not to control how many times it was replaced. Notice the & in the function definition. This is an output property, not an input property. Though I DO think it should act as both!
Proof.
<?php
$count = 2;
$str = str_replace("ll", "", "good golly miss molly dolly!", $count);
echo $count . ' ' . $str;
?>
Roonaan would be expecting output of ...
2 good goy miss moy dolly!
The actual output is ...
3 good goy miss moy doy!
Trying to run the code with a hard-coded value in the place of $count results in ...
Fatal error: Only variables can be passed by reference in C:\d6.php on line 3
To only replace the first occurance of a substring then use the preg_replace() function as described by kebabs.
http://www.php.net/manual/en/function.preg-replace.php
And just in case Roonaan wants to know what version I'm on ...
<29/03/2006 14:04:30 C:\>php -v
PHP 5.1.3-dev (cli) (built: Feb 20 2006 00:29:25)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
with Zend Extension Manager v1.0.10, Copyright (c) 2003-2006, by Zend Technologies
with Zend Optimizer v3.0.0-Beta2, Copyright (c) 1998-2006, by Zend Technologies
<29/03/2006 14:04:30 C:\>php -v
PHP 5.1.3-dev (cli) (built: Feb 20 2006 00:29:25)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies
with Zend Extension Manager v1.0.10, Copyright (c) 2003-2006, by Zend Technologies
with Zend Optimizer v3.0.0-Beta2, Copyright (c) 1998-2006, by Zend Technologies
$string = str_replace($old,$new,$str
Otherwise, try this quick and dirty one:
$string = implode('newstring', explode('occurance', $string, 2));
-r-