Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help with Curl and PregMatch

Status
Not open for further replies.

biobrain

MIS
Jun 21, 2007
90
0
0
GB
I want to curl a page and extract data between a div tag

<div class="topmasters" align="center"> data required </div>

here is my code
Code:
// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "[URL unfurl="true"]http://example.com/");[/URL]

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);


preg_match_all('/<div\s class=\'topmasters\' \s align=\'center\'>(.*?)<\/div>/",$output, $t);

$output=str_replace('/<div\s class=\'topmasters\' \s align=\'center\'>','',$t[0]);

echo $output;

?>

Please help to spot the error in the code?
 
If you put spaces around the \s characters, I think you are saying that you want three characters.

Furthermore, .* is greedy, as far as I know. So the closing tag is already within the (.*?) part.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
is this the pattern you're looking for?

Code:
$pattern = '/<div\s.*?class=(\'|")topmasters\1.*?>(.*?)</div>/ims';

 
I have updated my code as

Code:
<?php

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "URL.COM");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

$pattern = '/<div\s.*?class=(\'|")topmasters\1.*?>(.*?)</div>/ims';

preg_match_all('$pattern, $output, $t);

$output=str_replace('$pattern','',$t[0]);

echo $output;

?>
but it is not working. I guess still there is some problem
 
i have no idea what your code is supposed to do!

try this
Code:
<?php

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "URL.COM");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

$pattern = '/<div\s.*?class=(\'|")topmasters\1.*?>(.*?)</div>/ims';

preg_match_all($pattern, $output, $matches);
echo "<pre>";
print_r ($matches);
?>
the 'found' text will be in the [2] subkey of each match.
 
Here is my complete code. I want to fetch the media player streaming from the page.

I have tried again but this is still not working.

Code:
<?php

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "[URL unfurl="true"]http://desimadesi.webhop.org/pak%20Channels/playmovies.php?mid=46");[/URL]

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL, and return output
$output = curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);

$pattern = '/<div\s.*?class=(\'|")topmasters\1.*?>(.*?)</div>/ims';

preg_match_all($pattern, $output, $matches);
echo "<pre>";
print_r ($matches);
?>
 
Can you define "not working"?

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
can you provide the url you are working with?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top