using preg_replace on html entities

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

using preg_replace on html entities

Post 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>
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply