Forum Moderators: coopster

Message Too Old, No Replies

Get match() Problem

         

nandla

6:27 pm on Mar 3, 2011 (gmt 0)

10+ Year Member



Hi!
I want to extract remote file data between html tags thru following method using cURL.
<?PHP


//url
$url = 'http://abc.com/somefile.html';

//get the page content
$imdb_content = get_data($url);

//parse for product name
$name = get_match('/<div class="matchHeaderTeamText">(.*)<\/div>/isU',$imdb_content);
$team = get_match('/<div class="matchHeaderScore">(.*)<\/div>/isU',$imdb_content);
echo $team[0] . "<br /><br /><br />";

$team = get_match('/<p class="teamText">(.*)<\/p>/isU',$imdb_content);

$rr = get_match('/<div class="matchHeaderLeft">(.*)<\/div>/isU',$imdb_content,$imdb_content );

//build content
$content.= '<h2>Line: '.$name.'</h2>';
$content.= 'Run Rate ' .$rr. '';
$content.= '<br />Team ' .$team. '';

echo $content;

//gets the match content
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}

//gets the data from a URL
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}


Everything is working fine.
But the only issue is GET_MATCH() only matches first tags only (once).
I know it can be done thru preg_match_all but I dont know how to use according to above preg match.
Can some body write a little line code for me to match all data whereever these tags happen ?
Sorry for poor english.
Waiting for response.
cheers :)

coopster

11:26 pm on Mar 3, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, so you know that you need to modify your function first to preg_match_all:
//gets the match content 
function get_match($regex,$content)
{
preg_match_all($regex,$content,$matches);
return $matches[1];
}

... but what next? Are you asking how you might process the returned array results? Or are you asking how to synchronize the teams and scores, etc?

nandla

7:12 am on Mar 4, 2011 (gmt 0)

10+ Year Member



Thanx Coopster!
But until and unless we do not have multiple lines in array, what is the need to use preg_match_all() ?
The problem is, get_match finds only first line match :(

suppose
we have 2 lines like <div id="2">text 1</div><div id="2">text 2 </div>

get_match only gets the first div data not remaining matches

coopster

3:29 pm on May 18, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No, it will return all occurrences found because of the subpattern capture taking place. You can see by dumping the contents of the variable after running the code. Here is a working example to demonstrate:
function get_match($regex,$content) 
{
preg_match_all($regex,$content,$matches);
return $matches[1];
}
$imdb_content = <<<ENDOFHTML
<table>
<tr><td>Table Row 1</td></tr>
<tr><td>Table Row 2</td></tr>
<tr><td>Table Row 3</td></tr>
</table>
<p>Nothing here</p>
<table>
<tr><td>Table Row 4</td></tr>
<tr><td>Table Row 5</td></tr>
<tr><td>Table Row 6</td></tr>
</table>
<p>Nothing here</p>
ENDOFHTML;
$team = get_match('/<tr>(.*)<\/tr>/isU',$imdb_content);
print '<pre>';
print_r($team);
print '</pre>';
exit;