Jump to content

Php preg_match_all problem


sebasjuh

Recommended Posts

Hello,

 

I found this php forum with google in search for an answer to my question. I can't find the answer so I thought maybe I can try it on this forum....

 

This is my question:

I'll have a curl function to get the data from a page, now i'm want the data between the <span class="Title"></span> and the data between the tag <a class='Link'></a>. If I use preg_match_all for the span tag i'll get the results but how can I check on 2 html tags?

 

The code I have is this: (the $result value is the data from the curl command).

Is is possible to make a $pattern with 2 tag's to search for? Or how can I else solve it? Hope someone can help me with it.....

 

$pattern = '/<span\s+(?:.*?\s+)?class=([\"\'])Title\1\s*>\s*(?:.*?\s+)*?(.*?)\s*<\/span>/';

preg_match_all($pattern, $result, $matches, PREG_SET_ORDER);

foreach ($matches as $val) {
echo "Title: $val[0] <br>";
}
?>

Link to comment
Share on other sites

Try this one:

 

$pattern = '/<(a(?=[^>]+class=(\'|")Link(?:\\2))|span(?=[^>]+class=(\'|")Title(?:\\3)))[^>]+>(.*?)<\/\\1>/si';
preg_match_all($pattern, $result, $matches, PREG_SET_ORDER);

foreach ($matches as $val) {
   echo "Tag: {$val[1]} - {$val[4]}<br />";
}

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.