PHP Remove JavaScript
this should do it:
echo preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var);
/s is so that the dot . matches newlines too.
Just a warning, you should not use this type of regexp to sanitize user input for a website. There is just too many ways to get around it. For sanitizing use something like the http://htmlpurifier.org/ library
How to Remove JavaScript Remnants from String in PHP
Your javascript regex is off
you have:
$query_text_lower = preg_replace("/<script[^>]*>.*?< *script[^>]*>/i", "", $new_text);
You're not detecting </script> inside the returned document, so it's not removing the javascript code itself from the page, but when you call striptags, you are removing the tags, so they don't appear in your final output. However, I can't see the site you're pulling this from so I can't be 100% on that one.
Let me know if that makes sense. Basically, the way it looks to me is that your first regex isn't actually matching anything.
How do I strip all javascript out of an HTML document using PHP?
echo preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $var);
As shown here.
Remove JavaScript comments using Regex - PHP
Like you say, when you Minify your code using the function, all the HTML code be in single line, for that any code after a JS comments well be a comment to.
So You can use this PHP regex to remove inline JavaScript comment :
Your new function :
public static function sanitize_output($buffer) {
$search = array(
'#^\s*//.+$#m', //remove JS comment,
'/ {2,}/',
'/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s'
);
$replace = array(
'',
' ',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
strip_tags, remove javascript
You can remove < script >...< /script > from your string before any calculations:
$text = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $text);
Or another solutions (slower, but sometimes more correct) from remove script tag from HTML content:
$doc = new DOMDocument();
// load the HTML string we want to strip
$doc->loadHTML($html);
// get all the script tags
$script_tags = $doc->getElementsByTagName('script');
$length = $script_tags->length;
// for each tag, remove it from the DOM
for ($i = 0; $i < $length; $i++) {
$script_tags->item($i)->parentNode->removeChild($script_tags->item($i));
}
// get the HTML string back
$no_script_html_string = $doc->saveHTML();
How to remove Javascript Tag from Posted Content
For removing tags, you can use preg_replace
as
preg_replace("/<.*script.*>(.|\\n)*<\/script>/", "", $input_lines);
preg_replace("/<.*stylesheet.*>(.|\\n)*<\/stylesheet>/", "", $input_lines);
There is no need to escape <
and >
and you can use .*
instead of (.*?)?
. Also I am making it greedy
for probable nested tags by using (.|\\n)*
instead of (.|\\n)*?
Is there any way of stripping all javascript from a string in PHP?
That's how strip_tags works, eg:
$html = '<foo>hello<bar>world</bar></foo>';
$fixed = strip_tags($html, '<bar>');
echo $fixed;
outputs:
hello<bar>world</bar>
It doesn't understand the DOM, it doesn't understand javascript. it's essentially doing:
$fixed = str_replace('<script>', '', $html);
The only "smarts" it has is recognizing that tags can have attributes and deleting those as well.
If you want to remove a tag and all of its contents, then you should be using a DOM parser, and deleting the unwanted nodes (aka tags) and their children from the tree entirely.
Related Topics
PHP Function Imagettftext() and Unicode
How to Pass Value from One PHP Page to Another Using Session
Output Text File with Line Breaks in PHP
Call to Undefined Method Maatwebsite\Excel\Excel::Load()
Datetime Class VS. Native PHP Date-Functions
Pushnotification Server Side Implementation
How to Inject a Repository into a Service in Symfony
Getting Multiple Checkboxes Names/Id's with PHP
Stored Procedures, MySQL and PHP
PHP Unable to Load PHP_Curl.Dll Extension
Order by Before Group by Using Eloquent (Laravel)
Foreach Value from Post from Form