Page 1 of 1

using preg_replace on html entities

Posted: Sun Sep 24, 2006 6:26 pm
by HiddenS3crets
I parse a tutorial by running htmlentities to 'sanitize the tags', but there are some tags I need to have unmodified.

This is my code:

Code: Select all

$tutorial = htmlentities($tutorial);
$tutorial = preg_replace("/<(acronym|b|code|i|q|u)>/", "/<(acronym|b|code|i|q|u)>/", $tutorial);
What I want it to do is replace <acronym>, <b>, <code>, etc with <acronym>, <b>, <code>, etc.

The problem is that when it finds an occurance of one of these types of tags, it replaces it with /<(acronym|b|code|i|q|u)>/.

Is there a way to set some sort of place holder to the replacement regex knows what tag it's modifying?

ex.
Tutorial has <acronym> in it
htmlentities tranfers it to <acronym>
preg_replace finds <acronym>, remembers that 'acronym' is the text and replaces it with <acronym>

Posted: Sun Sep 24, 2006 7:51 pm
by aaronhall
Unless there's a shortcut in preg, you could always loop through an array of the tags you want to replace and call preg for each tag in the array. It's a sloppy hack, but it would work. Maybe some of the regex experts have some input.

Posted: Sun Sep 24, 2006 9:21 pm
by feyd
Your code suggests two patterns, but the second argument to preg_match() is supposed to be a replacement pattern, not a match expression.

Code: Select all

<$1>
is likely what you want, put in single quotes.