0

I want to remove some html/js tag using "preg_replace". Here's my html tag:

<style type=text/css>' );document.write( '@media Print' );document.write( '{' );document.write( 'BODY {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( 'TABLE {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( 'TR {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( 'TD {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( '}' );document.write( '</style>' );

I want to remove tags from <style type=text/css>' ) up to ( '</style>' );.

My code is like this:

$result3 = preg_replace("(\<style(.+?)<\/style>/", '', $result2);

But nothing gets removed when I run it.

I would appreciate your help. Also if you could explain the answer to me I would be grateful.

iqstatic
  • 2,322
  • 3
  • 21
  • 39

1 Answers1

-1

You need to use preg_replace like below (Tested Offline):

<?php
$string = "another text above here";
$string .= "<style type=text/css>' );document.write( '@media Print' );document.write( '{' );document.write( 'BODY {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( 'TABLE {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( 'TR {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( 'TD {' );document.write( 'DISPLAY: none; print: none' );document.write( '}' );document.write( '}' );document.write( '</style>' );";

$string = preg_replace("/<!--.*?-->/", "", $string);
$string = preg_replace("/\([^)]+\)/","",$string);

echo $string;
?>

// Below Output will come
another text above here
Amit Gupta
  • 2,771
  • 2
  • 17
  • 31