1

what is the best way to replace a string like this using preg_replace:

<a class="left" href="javascript:goBack()">Back</a>

It is an exact string that appears in many places, and I need it gone. I have tried preg_replace('#<a class="left" href="javascript:goBack()">Back</a>#','',$str);

and preg_replace('#<a class="left" href="javascript:goBack\\(\\)">Back<\\/a>#','',$str) but no joy.

any help gratefully received.

3 Answers 3

3

There is absolutely no need to use preg_replace if you know the exact string you want to match. Use str_replace:

$str=str_replace('<a class="left" href="javascript:goBack()">Back</a>','', $str);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi guys,I got the impression that str_replace may be phased out, is this not the case?
@Liam Bailey Not at all. Somebody completely misunderstood something. str_replace is a core method and will almost certainly stay forever. Google codesearch alone can find 280000 scripts that use it, so removing it would lead to greater incompatibilities than updating from php3 to php6.
1

Why don´t you just use str_replace()?

Comments

0

If there's a reason to use preg_replace instead of str_replace, try this:

preg_replace('#\Q<a class="left" href="javascript:goBack()">Back</a>\E#','',$str);

That will cause any metacharacters (like the ( and ) in the string :-) ) to be treated as normal characters devoid of their special meanings.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.