Reputation: 15229
I'm working on implementing a way to allow HTML in a field that I have with PHP. The characters are saved to the database as they are inputted, so when I print them to the screen later on, I want the HTML to be respected (security is not an issue here, so please don't make it one). What I need right now is a way to change any "
instances to the proper "
character, but only when it falls inside an HTML declaration (i.e., <
and >
characters). When it's not inside these characters, it shouldn't be changed.
What is the proper way to do something like this with regular expression?
Upvotes: 0
Views: 144
Reputation: 75272
Try this:
$str = preg_replace('/"(?=[^<>]*+>)/', '"', $str);
From your description I gather you mean "inside an HTML tag" (declaration doesn't really mean anything in this context). I'm also assuming there can't be any '<'
or '>'
characters inside the tag (because they are legal in quoted attribute values).
The lookahead (?=[^<>]*+>)
asserts that there is a right angle bracket up ahead, and no other angle brackets between here and there. Assuming every angle bracket is part of a balanced pair, that should mean the current position is inside an HTML tag.
Upvotes: 2
Reputation: 25834
Something like this to find tags? X
is the character, but may be replaced by a string.
<[^>]*X[^>]*>
A practical example, which finds links that start with <a href
:
<a href="([^"]*)"[^>]*>.
Upvotes: 0