| parse html from url function, using different versions of php parse html from url function, using different versions of php |
drooh

msg:4409513 | 10:06 pm on Jan 21, 2012 (gmt 0) | 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; } |
|
|
penders

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

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

msg:4409525 | 11:23 pm on Jan 21, 2012 (gmt 0) | or rather print '<img src="<?=$html?>">';
|
penders

msg:4409620 | 2:26 pm on Jan 22, 2012 (gmt 0) | 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?
|
drooh

msg:4409640 | 5:10 pm on Jan 22, 2012 (gmt 0) | 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" />
|
drooh

msg:4409648 | 5:27 pm on Jan 22, 2012 (gmt 0) | 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?
|
penders

msg:4409664 | 6:19 pm on Jan 22, 2012 (gmt 0) | 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]
|
|
|