nevos
nevos

Reputation: 927

extracting links in regex php

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!

Upvotes: 1

Views: 86

Answers (1)

vks
vks

Reputation: 67968

(?<=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
        )

)

Upvotes: 1

Related Questions