CodeIgniter Forums

Full Version: Need help with regexp (or strip_tags maybe?)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hello everyone.

I'm playing with regexp with no luck.
Currently I have this string:
<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\">
What should I do I have a :
as a result?

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

Thank you in advance!

El Forum

why don't you use php support for work with DOM?

El Forum

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

El Forum

mihailt, I will look into it. didn't ever hear about it.

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

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