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.

8
  • $result3 = preg_replace("/<style type=text/css>/", "'</style>'", $result2); Commented Dec 7, 2017 at 5:05
  • You can do that with substr and strpos too. Commented Dec 7, 2017 at 5:09
  • Are you sure it shouldn't be $result3 = preg_replace("(<style(.+?)</style>)", '', $result2); Commented Dec 7, 2017 at 5:15
  • no one working. Commented Dec 7, 2017 at 5:19
  • @AlivetoDie--Anantsingh i want to remove all of these html tag, not replace it to </style>. </style> i want to remove it too. Commented Dec 7, 2017 at 5:21

1 Answer 1

-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
Sign up to request clarification or add additional context in comments.

2 Comments

Downvoter please explain in comment.
@ainodoramaaa2 Have you tried my solution?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.