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
7
  • 1
    This is a prime candidate for this link (somebody had to) - but in all seriousness, why are you processing raw ASP code in PHP? Commented May 24, 2012 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 : stackoverflow.com/questions/188414/best-xml-parser-for-php Commented May 24, 2012 at 10:58
  • 2
    @FailedDev rather stackoverflow.com/questions/3577641/best-methods-to-parse-html/… because this aint XML. In any case, this is likely a duplicate. Commented May 24, 2012 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!) Commented May 24, 2012 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 Commented May 24, 2012 at 11:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.