如何从段落中提取HTTP链接并将其存储在php的数组中 [英] How to extract http links from a paragraph and store them in a array on php

查看:75
本文介绍了如何从段落中提取HTTP链接并将其存储在php的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php的var中有一个大文本,我正在寻找一种好的且快速的方法来检索此文本中的所有链接并将它们存储到数组中.

I have a big text inside a var on php, im looking for a good and fast method to retrive all the links inside this text and store them into an array.

文本为纯ascii,链接为常见链接,如http://thesite.comhttp://www.thesite.com.感谢您的帮助.

The text is plain ascii and the links are the common ones like http://thesite.com or http://www.thesite.com. Thanks for any help.

推荐答案

$text = 'Lorem ipsum http://thesite.com dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt https://www.thesite.com ut labore et dolore magna aliqua. Ut http://www.thesite.com enim ad minim veniam,';

$pattern = '!(https?://[^\s]+)!'; // refine this for better/more specific results

if (preg_match_all($pattern, $text, $matches)) {
    list(, $links) = ($matches);
    print_r($links);
}

这篇关于如何从段落中提取HTTP链接并将其存储在php的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆