Function to replace first occurence only.

Posted on 2006-03-29
Medium Priority
Last Modified: 2010-05-16

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?
Question by:Coolkill
  • 2
LVL 49

Expert Comment

ID: 16319777
When your php version is right, you can use a 4th parameter to str_replace:

$string = str_replace($old,$new,$string,1);

Otherwise, try this quick and dirty one:

$string = implode('newstring', explode('occurance', $string, 2));


Accepted Solution

kebabs earned 280 total points
ID: 16320059
Or the method for optimum customisation and backwards compatibility (this works in PHP 4 and above):
$string = preg_replace($pattern, $replace, $string, 1);

Look at the php manual: http://php.net/manual
LVL 40

Expert Comment

by:Richard Quadling
ID: 16320907
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!


$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.

LVL 40

Expert Comment

by:Richard Quadling
ID: 16320928
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question