Disable javascript on page - php

Is there any way to disable javascript when generating a page with php?
Code:
<?php
$url = 'http://www.kupime.com/';
$data = file_get_contents($url);
$data = '<head><base href='.$url.' target="_blank" /></head>'.$data;
echo $data;
?>

Use regular expressions to remove everything that is inside <script> tags.
$text = preg_replace('/<script.+?<\/script>/im', "", $text);

check out this bookmarklet that prints all script elements in a page.
javascript:(function(){s=document.getElementsByTagName('SCRIPT');tx='';sr=[];for(i=0;i<s.length;i++){with(s.item(i)){t=text;if(t){tx+=t;}else{sr.push(src)};}};with(window.open()){document.write('<textarea%20id="t">'+(sr.join("\n"))+"\n\n-----\n\n"+tx+'</textarea><script%20src="http://jsbeautifier.org/beautify.js"></script><script>with(document.getElementById("t")){value=js_beautify(value);with(style){width="99%";height="99%";borderStyle="none";}};</script>');document.close();}})();
actually more useful would be to visit the source site http://jsbeautifier.org/

Related

PHP Simple HTML DOM Parser some pages issue

I have a code for read all inputs in a form.
The code works in my demo page an others, but not work in some pages.
For the example issue:
facebook:
$url = 'https://www.facebook.com';
$html = file_get_html($url);
$post = $html->find('form[id=reg]'); //id for the register facebook page
print_r($post);
Print an empty array.
Functional example:
$url = 'http://www.monografias.com/usuario/registro';
$html = file_get_html($url);
$post = $html->find('form[name=myform]');
print_r($post);
Print a form content
Facebook won't give you registration form directly, it will only respond with basic html, and the rest will be created with javascript. see for yourself
$url = 'https://www.facebook.com';
$html = file_get_html($url);
echo htmlspecialchars($html);
there is no form with "reg" ID in the html they send you.
simple_html_dom.php contains a line limiting the max file size it will parse:
define('MAX_FILE_SIZE', 600000);
For files larger than this size, file_get_html() will just return false.

Include with str_replace function?

I want to create a page with include function. This page should to grab another site and can change text or code with this function str_replace. I hope that it would be possible. I have written this code, but unfortunately it does not work:
<?php
$text = include('http://www.example.com/index.html');
$text = str_replace("<div id=\"hercss\">Hello.</div>", "<div id=\"mycss\">Welcome!</div>", $text);
echo $text;
?>
Maybe you have a solution? It would be fully appreciated by you.
Try this:
<?php
$url = "http://www.example.com/index.html";
$text = file_get_contents($url);
$text = str_replace("<div id=\"hercss\">Hello.</div>", "<div id=\"mycss\">Welcome!</div>", $text);
echo $text;
?>
Include only works with relative links I believe? The function file_get_contents works across domains

Error using preg_relace to change url youtube?

I have a sample code:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$pattern = '/http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)/i';
$replace = $pattern.'&w=550';
$string = preg_replace($pattern, $replace, $url);
?>
How to result is http://www.youtube.com/watch?v=KTRPVo0d90w&w=550
You can just append using the . operator:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$string = $url.'&w=550';
?>
Use preg_match instead:
<?php
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w&s=222';
$pattern = '/v=[^&]+/i';
preg_match($pattern, $url, $match);
echo 'http://www.youtube.com/watch?'.$match[0].'&w=550';
?>
Like below?
$url = 'http://www.youtube.com/watch?v=KTRPVo0d90w';
$bit = '&w=550';
echo "${url}${bit}";
Don't get me wrong, I'm not looking to gain any points here, but just thought I would add to this question and include a few options. I love toying with ideas like this every once in a while.
Using jh314's idea to concatenate the strings, thought that this could be used for future use, to actually replace a string inside the video's YouTube number, should the occasion ever present itself.
Such as $number for instance.
<?php
$url = 'http://www.youtube.com/watch?v=';
$number = 'KTRPVo0d90w';
$string = $url.$number.'&w=550';
// Output to screen
echo $string;
echo "<br>";
// Link to video
echo "Click for the video";
?>
The same could easily be done for the video's width.

how to print url from html code in php when url contain spaces

See i have an url in a html code
play
Now i want to print this url as it is written in a php page
http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
You can see that between the url 05 Dabangg Reloaded their is space. I made this program to print url from this html code..
$str = "play";
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
if (preg_match_all($pattern,$str,$matches))
foreach($matches[1] as $data)
{
$str=$data;
echo $str;
}
Then i am getting this
http://b48.ve.vc/b/data/48/3746/05
please do not mention on foreach($matches[1] as $data) line bcoz i am using it with so many urls.. I just want to know how to print the whole url in this format.
http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
Spaces are become a huge matter.. Do not know how to fix it..
What i need to add inside
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
For making it completely workable.
Please suggest me any idea.
$str = 'play';
$arr = explode("\"", $str);
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
$url = preg_grep($pattern,$arr);
$url = implode('',$url);
Output: $url = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3'
Update: 2nd Solution [Reference-DOMElement].
$str = 'play';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
$search_item = $DOM->getElementsByTagName('a');
foreach($search_item as $search_item) {
$url = $search_item->getAttribute('href');
}
echo $url; //Output: http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
You can str_replace each one -space- with %20 for encoding your URL
<?php
$url_org = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3';
$url_edited = str_replace(" ", '%20', $url_org);
?>
HERE
This will work.

How to get page title in php?

I have this function to get title of a website:
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}
However, this function make my page took too much time to response. Someone tell me to get title by request header of the website only, which won't read the whole file, but I don't know how. Can anyone please tell me which code and function i should use to do this? Thank you very much.
Using regex is not a good idea for HTML, use the DOM Parser instead
$html = new simple_html_dom();
$html->load_file('****'); //put url or filename
$title = $html->find('title');
echo $title->plaintext;
or
// Create DOM from URL or file
$html = file_get_html('*****');
// Find all images
foreach($html->find('title') as $element)
echo $element->src . '<br>';
Good read
RegEx match open tags except XHTML self-contained tags
Use jQuery Instead to get Title of your page
$(document).ready(function() {
alert($("title").text());
});​
Demo : http://jsfiddle.net/WQNT8/1/
try this will work surely
include_once 'simple_html_dom.php';
$oHtml = str_get_html($url);
$Title = array_shift($oHtml->find('title'))->innertext;
$Description = array_shift($oHtml->find("meta[name='description']"))->content;
$keywords = array_shift($oHtml->find("meta[name='keywords']"))->content;
echo $title;
echo $Description;
echo $keywords;

Categories