0

Using PHP, I would like to encode specific characters (the > and < characters) within the attribute values for any tag.

For example, <input type="text" data-name="Oliver<>Nassar"> would become <input type="text" data-name="Oliver&lt;&gt;Nassar">.

I'm not picky regarding the attribute name (eg. data-name or value attribute names). While I am happy to presume a whitespace character (\s) before the attribute value (eg. <input data-name="...">) it would be great if that were not a condition, such that I could meet cases such as <input type="text"data-name="Oliver<>Nassar">

I imagine PHPs preg_replace_callback will be made use of, but as far the actual expression, I could use some help there.

onassar
  • 3,313
  • 7
  • 36
  • 58
  • Any particular reason why you've added regex tag? – Alexei Levenkov Feb 20 '13 at 05:28
  • I believe I'll need a regular expression to match the contents of the attribute values. – onassar Feb 20 '13 at 05:29
  • Check out [this question](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) and see if you still want regex for that... – Alexei Levenkov Feb 20 '13 at 05:30
  • Have you considered [`htmlentites`](http://www.php.net/manual/en/function.htmlentities.php) or [`htmlspecialchars`](http://www.php.net/manual/en/function.htmlspecialchars.php)? – Passerby Feb 20 '13 at 05:31
  • @AlexeiLevenkov Thanks. I've stumbled into that many times. I understand the intention behind it, but maintain this listed question. – onassar Feb 20 '13 at 05:33
  • @Passerby I will use those if I am able to match the case using regex – onassar Feb 20 '13 at 05:34

1 Answers1

0

Here is the expression I came up with:

'/' .
    '(' .
        '\s{1}' .
        '[a-z\-]+' .
        '\s?' .
        '=' .
        '\s?' .
        '(' .
            '[\'|"]{1}' .
        ')' .
    ')' .
    '([^\2]*)' .
    '\2' .
'/iU',

Hope this is helpful to someone else.

onassar
  • 3,313
  • 7
  • 36
  • 58