tags:

views:

163

answers:

2

Hi,

I want to get the dynamic contents from a particular url:

I have used the code

echo $content=file_get_contents('http://www.punoftheday.com/cgi-bin/arandompun.pl');

I am getting following results:

document.write('"Bakers have a great knead to make bread."

') document.write('© 1996-2007 Pun of the Day.com
')

How can i get the string Bakers have a great knead to make bread. Only string inside first document.write will change, other code will remain constant

Regards,

Pankaj

+5  A: 

You are fetching a JavaScript snippet that is supposed to be built in directly into the document, not queried by a script. The code inside is JavaScript.

You could pull out the code using a regular expression, but I would advise against it. First, it's probably not legal to do. Second, the format of the data they serve can change any time, breaking your script.

I think you should take at their RSS feed. You can parse that programmatically way easier than the JavaScript.

Check out this question on how to do that: Best way to parse RSS/Atom feeds with PHP

Pekka
Hi, Thanks for your reply but i want to know if i use their rss feed how can i pick the pun of the day dynamically (it should not repeat if i click on refresh)
Pankaj Khurana
I think reading the RSS feed, and displaying only the first item should do the trick!
Pekka
But i want to display different puns if he click on refresh.
Pankaj Khurana
That is also easy to achieve by picking a random pun from the RSS feed. It's not possible with the JS solution because it shows only the pun of the day.
Pekka
I am using this code $xml = simplexml_load_file("http://feeds.feedburner.com/PunOfTheDay");$items = count($xml->channel->item);echo $rand=rand(0,$items-1);$description = $xml->channel->item[$rand]->description;echo str_replace('[Click to Vote!]','',$description)Is this is correct approach
Pankaj Khurana
I don't know, I can't test it right now. Does it work?
Pekka
Ya but since the no of puns is less the probability of repetition is very high but thanks for your suggestion
Pankaj Khurana
Right, I see now! I didn't realize you are fetching a random pun from the Perl script. Sorry! Try talking to them whether there is any way to include more puns to the RSS stream, or serve a random pun through RSS. Otherwise, you may indeed be best off with @Luca Matteis' suggestion.
Pekka
+2  A: 

Pekka's answer is probably the best way of doing this. But anyway here's the regex you might want to use in case you find yourself doing something like this, and can't rely on RSS feeds etc.

document\.write\('      // start tag
([^)]*)                 // the data to match
'\)                     // end tag

EDIT for example:

<?php
$subject = "document.write('&quot;Paying for college is often a matter of in-tuition.&quot;<br />')\ndocument.write('<i>&copy; 1996-2007 <a target=\"_blank\" href=\"http://www.punoftheday.com\"&gt;Pun of the Day.com</a></i><br />')";
$pattern = "/document\.write\('([^)]*)'\)/";
preg_match($pattern, $subject, $matches);
print_r($matches);
?>
Luca Matteis
hi, can you please elaborate it further
Pankaj Khurana
@Pankaj: I've edited my answer, I hope that helps.
Luca Matteis
+1 for answering the question.
Pekka
Thanks but it is giving Warning: preg_match() [function.preg-match]: Unknown modifier 'g'Removing g gives the correct output.
Pankaj Khurana
@Pankaj: ok, i guess i forgot how php's regex implementation works
Luca Matteis