在regex-php中提取链接


extracting links in regex php

使用preg_match_all()在正则表达式中提取链接时遇到问题。

我有以下字符串:

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>

我想将文件的链接和文件格式提取到两个单独的变量中。

这里有正则表达式大师吗?我一整天都在为此而挣扎。

谢谢!

(?<=href=")(.*?'.(.*?))''

试试这个。只需抓住照片。请参阅演示。

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

输出:

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