0

I am trying to do a replace within a string in PHP. How do you delete the part that is only in the group in PHP?

<font.+?(size.+?.)>

I want to remove size=x where ever it in. The problem is I cannot get the

 $text = preg_replace("<font.+?(size.+?.)>","",$text);

function to work.

Example source of this

<font style="background-color: rgb(255, 255, 0);" size="2"><strong><u>text</u></strong></font>
<font size="2">more text</font>

into this

<font style="background-color: rgb(255, 255, 0);" ><strong><u>text</u></strong></font>
<font>more text</font>

I am trying to say. Where ever there is a font tag and if I see size-anything remove the size attribute, but leave everything else intact.

3 Answers 3

4
$dom = new DOMDocument();
$dom->loadHTML($htmlstring);
$x = new DOMXPath($dom);
$list = $x->query('//font/@size');
for($i = $list->length-1;$i>=0;$i--){
    $attr = $list->item($i);
    $attr->ownerElement->removeAttributeNode($attr);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. I wasn't aware of the DOMDocument() class.
1

Not the best solution, but to answer your question:

$html = <<<END
<font style="background-color: rgb(255, 255, 0);" size="2"><strong><u>text</u></strong></font>
<font size="2">more text</font>
END;

$text = preg_replace('/(<font.*?)(size\s*=[^\s>]*)([^>]*)>/si', '\1\3>', $html);
var_dump($text);

Comments

1

Regex is a poor way of doing HTML manipulation, but that said, the general technique to do this kind of regex matching and partial replacement is to match:

(<font.+?)(size.+?.)(>)
\________/\________/\_/
    1         2      3

And then replace with

$1$3

This substitutes into the replacement backreferences to what group 1 and group 3 matched, leaving group 2 out, effectively deleting what group 2 matched.

References

3 Comments

While in essence the use of capturing subpatterns would work, the regex itself is of course insufficient. Size not as a last attribute, size somewhere in a attribute value, > somewhere in a attribute value, size in another element after the font tag, etc.
@Wrikken: correct, hence the disclaimer about regex for HTML. If somehow a pattern that works can be found (easier said than done), then the methodology described here can be applied.
Thank you for the responses. I am not modifying the actual html page. Someone has sorted data in a database and I need to strip out tags in order to override it with custom css so when it is displayed in the page the <font> element will take on the look and feel we are looking for. Long story... Thank you again for all your help adn answers...

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.