Hiya! I have a script that displays HTML. How do I convert HTML Entity's that are not inside the "<" and ">" symbols. In the following example, only the items shown in bold and blue should be coverted to their HTML equivalent codes. e.g. £ <div name="divname" style="width: 200px; height: 100px;">[COLOR=red]I[COLOR=blue][B]'[/B][/COLOR]ve got to save[B] [COLOR=blue]£[/COLOR][/B]3000 in order to buy a new car[B][COLOR=blue]![/COLOR][/B][/COLOR]</div> Code (markup): Many Thanks,
Here is an example how to do it, but it doesn't convert the symbol ! because it hasn't entity: $text = "<div name=\"divname\" style=\"width: 200px; height: 100px;\">I've got to save £3000 in order to buy a new car!</div>"; $chars = get_html_translation_table(HTML_ENTITIES,ENT_QUOTES); unset($chars["<"]); unset($chars[">"]); foreach(array_keys($chars) as $key){ $text = preg_replace('/'.$key.'(?![^<]*[>])/i', $chars[$key], $text); } echo($text); PHP:
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES); unset($trans['<'], $trans['>']); $str = <<<eof <div name="divname" style="width: 200px; height: 100px;">I've got to save £3000 in order to buy a new car!</div> eof; echo strtr($str, $trans); PHP: or function convert2entities($str) { //temporarily replace with placeholders... $str = str_replace(array('<', '>'), array('{OPEN}', '{CLOSE}'), $str); $str = htmlentities($str, ENT_QUOTES); //remove the placeholders... return str_replace(array('{OPEN}', '{CLOSE}'), array('<', '>'), $str); } $str = <<<eof <div name="divname" style="width: 200px; height: 100px;">I've got to save £3000 in order to buy a new car!</div> eof; echo convert2entities($str); PHP: Both are untested.