Hi

I'm struggling with preg _match and a regex for it.
I understand the concept of it but to actually make the regex is a whole different story.
I found this regex on the web and it works great but lacks one option.

What i want to do is filter an url from a piece of text and save that url.

I am on the right track but need some help with the regex.

Say i have this piece of text (html)

<a href="http://www.meandmymac.net">meandmymac.net</a>

And pull that through this php:

preg_match('/(http:\/\/|https:\/\/|mailto:)(www.|)[\d\w-]{2,63}\.[A-Z]{2,4}(\.[A-Z]{2,4}|)/i', $string, $match);

I get:

http://www.meandmymac.net

Which is great.

But if the url looks like this:

<a href="http://www.meandmymac.net?item=value">meandmymac.net</a>

I still get:

http://www.meandmymac.net

I want to make it so that the ?item=value bit stays on.
Obviously the regex needs to be changed in some way. But i can't figure it out.

Any one have an idea?
Thanks!

    Might it not be easier to just grab everything between the quotes?

    if(preg_match('/(?<=href=&quot;).*?(?=&quot;)/i', $string, $matches)) {
       echo $matches[0];
    }
    

    Note: B[/B] is a look-behind assertion, and B[/B] is a look-ahead assertion.

      @ - I've seen that, i don't really see how that helps me. Did you mean to break down the url and recreating it again? From what i gather this only works if there is no other code and characters around it.

      @ - That's actually quite a good idea. However the regex doesn't work in php. When i try it in this site: http://www.myregextester.com/index.php

      It seems to work. That site, as you can see, also offers an option to generate the php for it. Which also doesn't work. But with your idea, i think i can find a working solution 🙂

      I'll post here again if i need more help on this item. Cheers!

        Write a Reply...