Welcome Guest, Not a member yet? Register   Sign In
Need help with regexp (or strip_tags maybe?)
#1

[eluser]hugle[/eluser]
Hello everyone.

I'm playing with regexp with no luck.
Currently I have this string:
Code:
<td style=\"padding: 0in 5.4pt; width: 0.5in; border: medium 1.5pt 1.5pt medium none solid solid none -moz-use-text-color windowtext windowtext -moz-use-text-color;\" width=\"48\">
<strong>7n./6d.</strong>
</td>
What should I do I have a :
Code:
<td><strong>7n./6d.</strong></td>
as a result?

I'm sure some of you are smarter on this than me.

Thank you in advance!
#2

[eluser]mihailt[/eluser]
why don't you use php support for work with DOM? http://www.php.net/manual/en/book.dom.php
#3

[eluser]NogDog[/eluser]
If the goal is remove any style attribute from any TD tag:
Code:
$text = preg_replace('#(<td\s[^>]*)style=([\'"]).*\2([^>]*>)#isU', "$1$3", $text);
#4

[eluser]hugle[/eluser]
mihailt, I will look into it. didn't ever hear about it.

NogDog: thanks, but your example didn't work..
I found different sollution:

Code:
$text = '<a href="" style="font-size: 25px;">test</a>';

  function strip_tags_attributes($sSource, $aAllowedTags = array('<a>'), $aDisabledAttributes = array('style'))
    {
        if (empty($aDisabledAttributes)) return strip_tags($sSource, implode('', $aAllowedTags));

        return preg_replace('/<(.*?)>/ie', "'<' . preg_replace(array('/[removed][^\"\']*/i', '/(" . implode('|', $aDisabledAttributes) . ")[ \\t\\n]*=[ \\t\\n]*[\"\'][^\"\']*[\"\']/i', '/\s+/'), array('', '', ' '), stripslashes('\\1')) . '>'", strip_tags($sSource, implode('', $aAllowedTags)));
    }
    
    $text=strip_tags_attributes($text);
echo $text; // outputs: <a href="">test</a>

so here in $aAllowedTags = array('<a>') you put tags, you want to keep.
and in: $aDisabledAttributes = array('style') you put attributes you want to cut.
for example: $aDisabledAttributes = array('onabort', 'onblue', 'onchange', 'onclick', 'ondblclick', 'onerror', 'onfocus', 'onkeydown', 'onkeyup', 'onload', 'onmousedown', 'onmousemove', 'onmouseover', 'onmouseup', 'onreset', 'onresize', 'onselect', 'onsubmit', 'onunload') etc...

Hope it helps someone who hits this post Smile




Theme © iAndrew 2016 - Forum software by © MyBB