1

I have a string like string(8234) "<style>.{ margin-bottom:10px; float:left; display: inline-block; width:56%; text-align:left; padding-right:20px; padding-left:20px; } . > p { display: table-cell; height: 150px; vertical-align: middle; }..................</style>................. I want to remove <style> tag and all its contents. I have tried

$description = $product_info['description']; // the string with style tag
$text = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', ' ', $description );
echo $text;

but it shows the same string without removing <style> tag. I have also tried to replace only <style> tag by doing

$text    = str_replace("<style>", "", $description);

but this also doesn't work at all. I am seeing the same string again and again. I have also tried it with DOMParser

 $doc =   new DOMDocument();
 $doc->loadHTML($description);
 $xpath = new DOMXPath($doc);
 foreach ($xpath->query('//style') as $node) {
    $node->parentNode->removeChild($node);
 }
 $text    =    $doc->saveHTML();

but in all cases, output is the same with <style> and its contents

baig772
  • 3,404
  • 11
  • 48
  • 93

1 Answers1

0

Your regex does work, but might I suggest a simpler pattern, using non-greedy quantifers:

<style>.*?<\/style>
Scott Weaver
  • 7,192
  • 2
  • 31
  • 43
  • I am seeing this error, could you please write the complete line?`Unknown modifier .` – baig772 May 20 '16 at 18:07
  • check the IDEone links. One is yours, one is the simpler pattern. – Scott Weaver May 20 '16 at 18:08
  • unknown modifier might indicate unescaped pattern delimiters (`/`) should be (`\/`) ? see http://stackoverflow.com/a/5589826/244811 – Scott Weaver May 20 '16 at 18:09
  • well it works on IDEone, but it is not working for my `string` should I paste the complete string here? – baig772 May 20 '16 at 18:11
  • modify the IDEone with your input string, and give the link. – Scott Weaver May 20 '16 at 18:12
  • http://ideone.com/q9JjxV is the link. I don't know what's wrong here, it is working on IDEone but not on my system. I am getting the string directly from `db`, Do I have to do something to make the string in raw form? – baig772 May 20 '16 at 18:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112532/discussion-between-baig772-and-sweaver2112). – baig772 May 20 '16 at 18:18