views:

27

answers:

3

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 i tact.

Thanks again...

+1  A: 

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

polygenelubricants
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
@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.
polygenelubricants
@polygenelubricants: yes *if*, then the subpatterns would work :)
Wrikken
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...
nitefrog
+4  A: 
$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);
}
Wrikken
Thank you for the reply. I wasn't aware of the DOMDocument() class.
nitefrog
+1  A: 

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);
Inigoesdr