Question

How i can remove the size attribute and face attribute only in html font tag using PHP.

<font color="#ff0000" size="4" face="georgia"> text text again</font> 
again and again text <font color="#ff0000"> text text again </font>

by the way. im using wysiwyg editor thats why is in font tags and i only want to display the string without the size and face attribute.

Was it helpful?

Solution

<font> tags are deprecated. Use <span> instead.

To answer your question: you can remove the attributes using DOMDocument:

$html = '<font color="#ff0000" size="4"> text </font>';

$dom = new DOMDocument;
$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('font') as $tag) {
    $tag->removeAttribute('size');
    $tag->removeAttribute('face');
}

echo $dom->saveHTML($tag);

Output:

<font color="#ff0000"> text </font>

Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top