PHP Check if page contains - php

I'm looking for a quick code/function that will detect if a page contains a certain thing.
It's for a new project I'm working on.
Basically, the user will paste a simple javascript code into their pages, but I need to make sure they do.
I need a code that will scan through a specific webpage url and find the code I provided.
Thanks!

You want to scan through a webpage, not an URL! You get to the webpage through an URL. :)
<?php
$contents = file_get_contents("http://some.site/page.html");
$search = <<<EOF
<script type="text/javascript">
alert('They must have this!');
</script>
EOF;
if (strpos($contents, $search) === FALSE) {
echo "Naughty webpage!";
}
?>
Note, though, that programmatically skimming pages like this is generally considered bad form.

You can get the contents of a URL as a string, and search the contents for that code:
<?php
function check_url($url) {
$page = file_get_contents($url);
$code = '<script src="http://example.com/test.js"></script>';
if (strpos($page, $code) === FALSE) {
return false;
} else {
return true;
}
}
?>
You may want to swap that simple strpos out for a regular expression, but this will do the trick.

You need to do the 2 things:
1) get the content of remote url
2) check if the content contains your string:
if ( stristr($content, 'your_desired_string') )
{
echo ' Yes, found';
}

there are great libraries for crawling websites like cURL but in your case it seems to be an overkill to use it. If you want to use the cURL library I recommend the Snoopy-class to you which is very simple to use.
Felix

Related

echo content data from url source

Hey guys I'm not too knowledgeable about this particular topic in PHP yet. Basically I wanted to get a certain content of the url source so for instance the code below will only echo that specific content from the source page. I wanted to do this for other websites and the script below has errors but that's just like a demo of what I want to accomplished.
<?php
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
$data = getBetween($data,'<p class="c"> <font size="+2">',"</font></p>");
echo $data;
?>
All the information of the script above is located here
Use Simple HTML DOM to do this.
Read the manual to do this from here.
Its pretty simple.
//include simple_html_dom.php file.
include('../simple_html_dom.php');
// get DOM from URL or file
$html = file_get_html('http://www.jokesclean.com/OneLiner/Random/');
foreach($html->find('p[class=c]') as $e)
echo $e;
Just tested on my local system and it worked perfectly generating a random joke everytime i refresh
here's what i got on last refresh of this code.
.
It'd be best to use domdocument but you can also do it using regular expressions like the following.
$data = file_get_contents('http://www.jokesclean.com/OneLiner/Random/');
if ( preg_match('/\<font size="\+2"\>(.*?)\<\/font\>/', $data, $match) ) {
echo $match['1'];
}
else {
echo 'couldn\'t find a match';
}

How To: PHP elseIf to change div depending on the URL Contents

Issue: I have a php file that I do not have access to but I need to be able to format it. It generates several different pages depending on the search. I have been able to change a div using php if else statements in the header that the php calls to. (See Below)
What I would like to do is have a statement that will change the div depending on what is in the url, now the issue with this is that one of the pages I need to change, has the "submitIt" in the url which is what I am using to change the first div.
I have tried to change the code so that it calls to a different portion of the url but because the url still contains the first bit "submitIt" it calls to that div, rather than the new div.
Is there a way to call the elseif statement where if it contains "submitIt and "MEDIA_TYPE_ID" then it would print out the new div?
<?php $url = $_SERVER["REQUEST_URI"];
if (strpos($url, "submitIt")) {
echo '<div style="width:560px;" class="app-h3 app-table">';
}elseif (strpos($url, "MEDIA_TYPE_ID")) {
echo '<div style="width:560px;" class="app-h3 app-table-none">';
}else {
echo '<div style="width:560px;" class="app-h3">';
}
?>
I think you are looking for the && operator. It means 'and'.
if (strpos($url, "submitIt") && strpos($url, "MEDIA_TYPE_ID")) {
// do something
}
If both of those are true, it will execute the code block.

Wordpress not showing up when using PHP via plugin?

I have installed a plugin (use PHP in posts) for Wordpress, and am having some issues.
Here is my code (though I don't think its the issue)
$usertoget = $_GET['player'];
$partone = "http://website.co.uk/path/to/php/file.php";
if(strlen($usertoget) != 0) {
$parttwo = "&player=";
$partthree = $usertoget;
$finalurl = $partone . $parttwo . $partthree;
print '<iframe src="$finalurl"></iframe>';
}
else {
print "<iframe src='/path/file.php'>";
}
The iframe gets printed perfectly and does what the target file should, but as you can see from this picture, messes up the formatting:
No sidebar;
No footer;
No floating header.
I have noticed that the page source finishes the iframe and does nothing else except close (</body></html>). There is a lot of stuff after it that should appear but doesn't.
Another issue is, using GET requests (/page/hello?player=9 and /page/hello/?player=9) returns a 404. Any ways to resolve this issue?
Does anybody know how I can fix this? I can post any more code if required :)
Any help would be appreciated :D Thanks!
If PHP suddenly stops sending the expected output, it usually means there's a PHP error. Is there anything in your logs? Have you tried turning WP_DEBUG on?
I'm surprised you say the iframe gets printed perfectly. This line
print '<iframe src="$finalurl"></iframe>';
looks wrong to me (though it won't cause PHP to error). If you have single quotes around a string, variables won't be evaluated. See this question for a little more detail.
You have mistake in concatenation of $finalurl .you can check you html its print the $finalurl variable as it is.Its didn't outputted its values .I had check it.So you just have to concatenate the $finalurl variable correctly .Here is your code which i had made corrected.
$usertoget = $_GET['player'];
$partone = "http://website.co.uk/path/to/php/file.php";
if(strlen($usertoget) != 0) {
$parttwo = "&player=";
$partthree = $usertoget;
$finalurl = $partone . $parttwo . $partthree;
print '<iframe src='.$finalurl.'></iframe>';
}
else {
print "<iframe src='/path/file.php'>";
}
if you have any query please comment bellow.

php within a php statement

Using Wordpress, i have a plugin that inserts a playable MP3 on the page.
To call that, along with the track details, this code is inserted;
<?php if (function_exists("insert_audio_player")) {
insert_audio_player("[audio:http://thewebsite.com/thetrack.mp3|artists=Artist|titles=Titles]");
} ?>
I would like to make this editable from the backend easily, by entering some meta-data. So this;
<?php meta('track-url'); ?>
Along with other various details would replace those that are above.
Unfortunately for me, this;
<?php if (function_exists("insert_audio_player")) {
insert_audio_player("[audio:<?php meta('track-url'); ?>|artists=Jack Presto|titles=Track 1]");
} ?>
obviously does not work! This is down to my lack of understanding if PHP - can anyone help?
Cheers!
Simple! Do this:
<?php if (function_exists("insert_audio_player")) {
$trackUrl = meta('track-url');
insert_audio_player("[audio:$trackUrl|artists=Jack Presto|titles=Track 1]");
} ?>
I can't tell if the meta() function prints to the screen or returns a string. If it returns a string, do:
<?php
if (function_exists("insert_audio_player")) {
insert_audio_player('[audio:' . meta('track-url') . '|artists=Jack Presto|titles=Track 1]');
}
?>
If it prints to the screen, it's a bit more complicated. Ideally, you would have a function that DOES return a string, but as a quick hack (if you're only getting paid for a quick fix) you could do something like
<?php
if (function_exists("insert_audio_player")) {
ob_start();
meta('track-url');
$meta = ob_get_contents();
ob_end_clean();
insert_audio_player("[audio:$meta|artists=Jack Presto|titles=Track 1]");
}
?>

How to handle HTML code that repeats a lot

I have some HTML code portions that repeat a lot through pages. I put this html code inside a function so that it is easy to maintain. It works perfectly. I, however feel this may not be very good practice.
function drawTable($item){
?>
HTML CODE HERE
<?php
}
I also run into the problem that when I want to return data using json the following won't work as it will be NULL:
$response['table'] = drawTable($item);
return json_encode($response);
What's the correct way to handle HTML code that repeats a lot??
Thanks
You may want to look into using templates instead of using ugly heredoc's or HTML-embedded-within-PHP-functions, which are just plain unmaintainable and are not IDE-friendly.
What is the best way to include a php file as a template?
If you have a repeating segment, simply load the template multiple times using a loop.
Although templates help with D.R.Y., the primary focus is to separate presentation from logic. Embedding HTML in PHP functions doesn't do that. Not to mention you don't have to escape any sort of quotes or break the indentation/formatting.
Example syntax when using templates:
$data = array('title' => 'My Page', 'text' => 'My Paragraph');
$Template = new Template();
$Template->render('/path/to/file.php', $data);
Your template page could be something like this:
<h1><?php echo $title; ?></h1>
<p><?php echo $text; ?></p>
function drawTable( $item ) { return '<p>something</p>'; }
function yourOtherFunction() {
$response['table'] = drawTable($item);
return json_encode($response);
}
Use this function definition
function drawTable($item){
return 'HTML CODE HERE';
}
Called with
print drawTable($item);
Which will also work for your json return value.

Categories