Jump to content

Limiting results using Regexp from a URL scraping query


DJphp

Recommended Posts

Hello,

 

Whilst scraping a web page for URL's with this expression:

$urlLink = "/<a[^>]+href=\"(showthread\.php\?s=[^\"]+)/i";

 

and using preg_match_all and printf to display the results

 

i get the results (showing a subset):

showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&t=26041

showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&t=26041&page=2

showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&p=274081#post274081

and so on.

 

what I would like to do is only capture URL's like the first line:

  showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&t=26041

 

 

and exclude the results line 2 and 3

showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&t=26041&page=2

showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&p=274081#post274081

 

 

That is, I only want to exclude anything like the following:

 

showthread.php?s=****&p=****

or

  showthread.php?s=****&t=****&page=****

 

and only display results with:

showthread.php?s=****&t=****

 

I just cannot seem to do this. My thoughts were to exclude any results that included "&p=" or "&page=".

I just cannot seem to do that.

 

any help would be appreciated.

 

DJphp

 

Link to comment
Share on other sites

Try something like this:

 

<pre>
<?php
$data = <<<DATA
<a href="showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&t=26041"></a>
<a href="showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&t=26041&page=2"></a>
<a href="showthread.php?s=21be590fe8a4b4e317a7fa54b3ff8230&p=274081#post274081"></a>
DATA;
preg_match_all('/<a[^>]+href="(showthread\.php\?s=(??!&p(?:age)?=)[^"])+)"/i', $data, $matches);
array_shift($matches);
print_r($matches);
?>
</pre>

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.