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
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);
?>

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.