如何从PHP中的内容获取链接

时间:2017-03-18 11:08:01

标签: php wordpress

我创建了一个帖子,其中包含锚标记的内容我想获得链接

形成帖子内容。存储在数据库中的数据如下: -

<a href="https://www.manresa-sj.com/wp-content/uploads/2017/02/PhotoOfSteve.png">all data is here for you</a>


WP_Post Object
(

    [post_content] => all data is here for you

)

我希望来自{_ 1}}

之类的Post_content链接

有可能吗?

1 个答案:

答案 0 :(得分:1)

如果您的意思是要从文本中提取图像链接。 使用正则表达式。

$string = '<a href="https://www.manresa-sj.com/wp-content/uploads/2017/02/PhotoOfSteve.png">all data is here for you</a>';

$pattern = '~(http.*\.)(jpe?g|png|[tg]iff?|svg)~i';

$m = preg_match_all($pattern,$string,$matches);

print_r($matches[0]);