0

I use this to clean html from font tag :

$html = '<font class="textmiddle" color="red">toto</font>' ;
$clean = preg_replace('/<font[^>]*>/', '', $html) ;
$clean = preg_replace('/<\/font>/', '', $clean) ;

It works like a charm.

But, when the html string is :

$html = '<font class="textmiddle" color="<%= color.importanttext %>">toto</font>' ;

Then the result is not the expected one :

">toto
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dude Lebowski
  • 159
  • 1
  • 10
  • 1
    This is a prime candidate for [this link](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) (somebody had to) - but in all seriousness, why are you processing raw ASP code in PHP? – DaveRandom May 24 '12 at 10:56
  • In short, while what you want can be done with regex, don't do it. Instead write maintenable robust code by using one of the available xml/html parsers. Check this too : http://stackoverflow.com/questions/188414/best-xml-parser-for-php – FailedDev May 24 '12 at 10:58
  • 2
    @FailedDev rather http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662 because this aint XML. In any case, this is likely a duplicate. – Gordon May 24 '12 at 11:02
  • If you have HTML in a SGML-esque serialization (angle brackets are valid in attributes), then you need a more complex regex. Entirely doable, but not worth the effort. People here are indoctrinated and playing dumb about parsing and extracting, but it is in fact best left to those who mastered regex. If you aren't, you shouldn't ask for some code fix (that you later won't be able to maintain!) – mario May 24 '12 at 11:03
  • I'm not processing ASP code in PHP. I writed a function in PHP to clean some html containing JSP tags... Of course I made $clean = preg_replace('/<\/font>/', '', $clean) ; for the end tag – Dude Lebowski May 24 '12 at 11:03
  • @Gordon Yeah I think the OP got the point anyway, but thanks ;) – FailedDev May 24 '12 at 11:04
  • PoC with DOM: http://codepad.viper-7.com/8Id0Ip – Gordon May 24 '12 at 11:10

1 Answers1

1

Try

<?php
    $html = '<font class="textmiddle" color="<%= color.importanttext %>">toto</font>' ;
    $clean = preg_replace('/<font\s.*">/SimU', '', $html) ;
    echo $clean;
?>

but notice that you get

toto</font>

in output.

Valeriy Gorbatikov
  • 3,459
  • 1
  • 15
  • 9