0

I've got the following str_replace code:

$filterString = "[++] has performed well. [++] is showing good understanding";

echo str_replace("[++]", "Name", $filterString);

It basically replaces all instances of [++] with Name. However, i would like to only replace the first instance of the [++] with Name and all other instances should say He

Any idea how i can do this?

Thanks

5 Answers 5

2

Using str_replace so that it only acts on the first match?

echo preg_replace('/\[\+\+\]/', 'Name', $filterString, 1);
Sign up to request clarification or add additional context in comments.

2 Comments

But there should be changes "[++]" => "He" as well?
Yes, sorry. Then we need 2 steps $filterString = preg_replace('/[\+\+]/', 'Name', $filterString, 1); $filterString = str_replace("[++]",'He',$filterString); echo $filterString;
0

Just to toss in a one-row solution:

$filterString = str_replace("[++]","He",preg_replace("/\[\+\+\]/", "Name", $filterString, 1));

------EDIT-----

$filterString = preg_replace_callback('/[.!?].*?\w/',
                              function($matches) { return strtoupper($matches[0]);},
                              str_replace("[++]","he",preg_replace("/\[\+\+\]/", "Name", $filterString, 1))); 

This will change all characters that start a sentence to uppercase, plus fix all your earlier issues.

3 Comments

Thanks, I've just realized that when replacing the ++ from the second instance onwards with the word He, the word He can either be with a capital H or a lower case h depending on if there is a fullstop before it. Any idea how I can do this?
I'll get back to you on that...about to eat dinner ;)
@JohnSmith A new edit was just posted...does this take care of your issues?
0

You can use preg_replace() instead of str_replace()

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

After this first replace, you can use str_replace() for all the other ++ to replace with "He".

Later edit: I checked and I saw that str_replace() has a limit parameter so you could use it too instead of preg_replace().

Comments

0

If you just want to replace [++] for "Name" once, you should use preg_replace using the limit parameter, and then you can replace the rest of the string:

$filterString = "[++] has performed well. [++] is showing good understanding. [++] is a good student.";
$filterString = preg_replace("/\[\+\+\]/",'Name',$filterString,1);
$filterString = str_replace("[++]",'He',$filterString);     
echo $filterString; //will print "Name has performed well. He is showing good understanding. He is a good student."

Comments

0

Try this...

$filterString = "[++] has performed well. [++] is showing good understanding.";

$newstr = substr_replace($filterString, "Name", 0, 4);

echo str_replace("[++]", "He", $newstr);

1 Comment

@John Smith: is this help you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.