Jump to content

Should I use preg_match?


christo16

Recommended Posts

Hello All,

I'm am pulling a page with file_get_contents and I want to use preg_match to search the page to find the url that matches the STRING:

<a href="http://www.example.com/[0-9][0-9][0-9][0-9]">STRING</a>

So I have whatever STRING is, but I want to find the URL which has some numbers on the end of it, represented by the [0-9]*4 at the end of the example.

Is preg_match the best for that?  Regex is confusing me, I've tried a few things and they didn't seem to work.

Any help is appreciated!

Thank you!

Link to comment
https://forums.phpfreaks.com/topic/76830-should-i-use-preg_match/
Share on other sites

Yes preg_match is your friend :)

<?php
   $str = 'some text and the link: <a href="http://www.example.com/yeah/20">test</a>';
   preg_match_all('/\<a href=".*?[\d]+">.*?</a>/', $str, $match);
   /*
      the string must contain a link starting with <a href=" followed by any sign
      then followed by a digit and ends up with ">, any sign and </a>
   */
   print_r($match);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.