Welcome to WebmasterWorld Guest from 54.167.252.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

parse html from url function, using different versions of php

parse html from url function, using different versions of php

     
10:06 pm on Jan 21, 2012 (gmt 0)

5+ Year Member



Greetings!

I have a function that works in php v 5.3 and above but wondering what is the best method for doing this with version below 5.3. I'm looking at str_replace() as an option?

Here is my function for php v5.3 and above

function parseHTML($url,$start,$end,$trimStart,$trimEnd){
$html = file_get_contents($url);
$html = strstr($html,$start);
$html = strstr($html,$end, true);
$html = substr($html,$trimStart);
$html = substr($html,0,$trimEnd);
return $html;
}
11:00 pm on Jan 21, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Can you give an example of how this function is called and the expected outcome?
11:22 pm on Jan 21, 2012 (gmt 0)

5+ Year Member



$html = parseHTML('http://wwc.instacam.com/showcam.asp?id=ASPLS&size=S','<img name="animImg" src=','</td>',25,-29,1);
echo $html;
11:23 pm on Jan 21, 2012 (gmt 0)

5+ Year Member



or rather
print '<img src="<?=$html?>">';
2:26 pm on Jan 22, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



It kinda looks like a regex would better suit this, but anyway, maybe your needs are different?

$html = strstr($html,$end, true);


This is the only line that fails in earlier versions of PHP (before 5.3) because of the extra 3rd argument. This can be rewritten using substr and strpos to work in PHP 4+

$html = substr($html,0,strpos($html,$end)); // PHP 4+ alternative


The only caveat with this is that substr() will return an empty string, rather than (bool)false if $end is not found - but this shouldn't matter in this example.

You mention str_replace() - how were you intending to use this?
5:10 pm on Jan 22, 2012 (gmt 0)

5+ Year Member



function parseHTML($url,$start,$end,$trimStart,$trimEnd,$phpVersion){
$html = file_get_contents($url);
$html = strstr($html,$start);
if($phpVersion){
$html = strstr($html,$end, true); // v5.3+
} else {
$trim = strstr($html,$end); // v5.2-
$html = str_replace($trim,'',$html);
}
$html = substr($html,$trimStart);
$html = substr($html,0,$trimEnd);
return $html;
}


Call (notice that $phpVersion is either true or false, meaning that it is 5.3 or greater)
<img src="<?=parseHTML('http://wwc.instacam.com/showcam.asp?id=SNNHP&size=S','<img name="animImg" src=','</td>',25,-29,1)?>" alt="" width="" height="" border="0" />
5:27 pm on Jan 22, 2012 (gmt 0)

5+ Year Member



I think I like the strpos option better, less code and would seem faster too, thanks for the help!

would there be any other options for performing this task?
Essentially parsing a URL for an img?
6:19 pm on Jan 22, 2012 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Using a regular expression...
$html = file_get_contents($url); 
preg_match('/<img name="animImg" src="([^"]+)"/',$html,$matches);
echo $matches[1];


$matches[1] contains the value of the src attribute. And is more flexible as to its location in the document. And works in all versions of PHP.

You never need to pass the php version to a function, since this can be read from the system... phpversion() [uk3.php.net]
 

Featured Threads

Hot Threads This Week

Hot Threads This Month