1

I'm having issues extracting links in regex using preg_match_all().

I have the following string:

some random text <a href="http://localhost/example/wp-content/uploads/2014/07/Link1.pdf\">Link1</a><a href="http://localhost/example/wp-content/uploads/2014/07/Link2.pdf\">Link2</a>

I would like to extract the link to the files and the files format into two separate variables.

Any regex gurus here? I've been struggling with this all day.

Thanks!

1 Answer 1

1
(?<=href=")(.*?\.(.*?))\\

Try this.just grab the captures.see demo.

http://regex101.com/r/lS5tT3/80

$data = 'some random text <a href="http://localhost/example/wp-content/uploads/2014/07/Link1.pdf\">Link1</a><a href="http://localhost/example/wp-content/uploads/2014/07/Link2.pdf\">Link2</a>"';
$regex =  '/(?<=href=")(.*?\.(.*?))\\\\/';
preg_match_all($regex, $data, $matches);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => http://localhost/example/wp-content/uploads/2014/07/Link1.pdf\
            [1] => http://localhost/example/wp-content/uploads/2014/07/Link2.pdf\
        )

    [1] => Array
        (
            [0] => http://localhost/example/wp-content/uploads/2014/07/Link1.pdf
            [1] => http://localhost/example/wp-content/uploads/2014/07/Link2.pdf
        )

    [2] => Array
        (
            [0] => pdf
            [1] => pdf
        )

)
Sign up to request clarification or add additional context in comments.

8 Comments

That's exactly what I need. But for some odd reason using this expression in preg_match_all() doesn't work for me.. It returns NULL. Any ideas?
@Nevos use groups(), cpature group1 for link ,group2 for format
What's groups()? I can't find reference to it
@AvinashRaj thanx a lot.but why are the two links appearing two times
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.