PHP(如果字符串包含URL)将其隔离 [英] PHP if string contains URL isolate it

查看:59
本文介绍了PHP(如果字符串包含URL)将其隔离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我需要确定字符串是否包含URL.如果有URL,则需要将其隔离为另一个单独的字符串.

例如:"SESAC表演爱情! http://twitpic.com/1uk7fi "

我需要能够将该字符串中的URL隔离为一个新字符串.同时,URL必须在原始字符串中保持完整.跟随吗?

我知道这可能真的很简单,但是它正在杀死我.

解决方案

类似

preg_match('/[a-zA-Z]+:\/\/[0-9a-zA-Z;.\/?:@=_#&%~,+$]+/', $string, $matches);

$matches[0]将保存结果.

(注意:此正则表达式当然不符合RFC;它可能会提取格式错误的URL(按照规范).请参阅http://twitpic.com/1uk7fi"

I need to be able to isolate the URL in that string into a new string. At the same time the URL needs to be kept intact in the original string. Follow?

I know this is probably really simple but it's killing me.

解决方案

Something like

preg_match('/[a-zA-Z]+:\/\/[0-9a-zA-Z;.\/?:@=_#&%~,+$]+/', $string, $matches);

$matches[0] will hold the result.

(Note: this regex is certainly not RFC compliant; it may fetch malformed (per the spec) URLs. See http://www.faqs.org/rfcs/rfc1738.html).

这篇关于PHP(如果字符串包含URL)将其隔离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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