1

want to remove all css rules from inline css but not to remove display none property

for example,

 <p style="color:red;display:none;float:left;">

I Tried this I am using php simple HTML DOM parser

and snippet is here

 foreach ($obj->find('img') as $e) {
        $imgSrc = $e -> src;
        preg_match_all('~' . SITE_NAME . '~is', $imgSrc, $match);
        if (count($match[0]) == 0) {
            $loadedSrcs = SITE_NAME . $imgSrc;
        } else {
            $loadedSrcs = $imgSrc;
        }

        $currentImageSrc = $e -> outertext;
        $replacementImageSrc = "src='" . $loadedSrcs . "'";
        $changeSrc = preg_replace('/src=[\'|\"][^\'|\"]*[\'|\"]/is', $replacementImageSrc, $currentImageSrc);
        //$imageSrc = "<img src=".$changeSrc." />";

       $ImagesWithFullPath = preg_replace('~<img [^c]*c=[\'|\"]'.$e -> src.'[^\>]*\>~is', $changeSrc , $ImagesWithFullPath);

    // now I want to remove diffrent style property from Image and wandering how could I write the preg_replace not display:none 

}

Hope it helps to understand my Query

I want to do it with preg_replace(php) not with Jquery.

Any help will be highly appreciated.

5
  • strip the whole paragraph tag an insert a new one: <p style="display: none;"> Commented Sep 13, 2012 at 13:53
  • Don't try parsing HTML with regexps. Use a proper HTML parser instead. Commented Sep 13, 2012 at 13:54
  • I tried with preg_replace but stuck Commented Sep 13, 2012 at 13:54
  • just put an if condition if (1) <p style="color:red;display:none;float:left;"> else <p style="display:none"> Commented Sep 13, 2012 at 13:55
  • @Ilmari Karonen I already usinf PHP SIMPLE DOM PARSER I am Posting my code here Commented Sep 13, 2012 at 13:56

1 Answer 1

1

Once you get the proper element with an HTML parser:

$styles = array_map('trim', explode(';', $e->style));
if (in_array('display:none', $styles)) {
    $e->style='display:none';
}
else {
    $e->style='';
}
Sign up to request clarification or add additional context in comments.

Comments

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.