Dat Nguyen
Dat Nguyen

Reputation: 74

delete a string w/ certain tags using preg_replace

i have this string

$output=' i want to <a class="SForm" onclick="ShowFormComment()"><img src="http://vnexpress.net/Images/Y-kien-cua-ban.gif" border="0" style="cursor:pointer" alt="Ý kiến của bạn" /> kiss you so much';

and i want to get

' i want to kiss you so much'

so i tried

$output =preg_replace('/<a class="SForm" class=(.*?)\Ý kiến của bạn" />/', '', $output);

but it didnt work

Warning: preg_replace(): Unknown modifier '&' in /Users/datlap/Desktop/str.php on line 10

Any idea?

Upvotes: 0

Views: 339

Answers (2)

Jacob
Jacob

Reputation: 8344

You don't need to use regular expressions for something like that...

A much simpler solution would be to use

$output = strip_tags(htmlspecialchars_decode($output));

https://www.php.net/manual/en/function.htmlspecialchars-decode.php

https://www.php.net/manual/en/function.strip-tags.php

Upvotes: 1

codaddict
codaddict

Reputation: 455440

To make it work escape the / before &.

$output =preg_replace('/<a class="SForm" class=(.*?)\Ý kiến của bạn" \/>/', '', $output);
                                                                                       ^

You are using / as the regex delimiter and there is a unescaped / in the regex which is being treated as the end delimiter.

Upvotes: 1

Related Questions