Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用于字符串中所有URL的正则表达式,不捕获带问号的URL_Php_Regex_Url - Fatal编程技术网

Php 用于字符串中所有URL的正则表达式,不捕获带问号的URL

Php 用于字符串中所有URL的正则表达式,不捕获带问号的URL,php,regex,url,Php,Regex,Url,我从这个PHP字符串开始 $bodyString = ' another 1 body reg http://www.regularurl.com/home secure https://facebook.com/anothergreat. a subdomain http://info.craig.org/ dynamic; http://www.spring1.com/link.asp?id=100408 www domain; at www.

我从这个PHP字符串开始

$bodyString = '
    another 1 body
    reg http://www.regularurl.com/home
    secure https://facebook.com/anothergreat.
    a subdomain http://info.craig.org/
    dynamic; http://www.spring1.com/link.asp?id=100408
    www domain; at www.wideweb.com
    single no subdomain; simple.com';
需要将所有域、URL转换为锚(
,$bodyString)

$bodyString
结果:

'another 1 body
    reg <ahref="http://www.regularurl.com/home">http://www.regularurl.com/home</a>
    secure <a href="https://facebook.com/anothergreat.">https://facebook.com/anothergreat.</a>
    a subdomain <a href="http://info.craig.org/">http://info.craig.org/</a>
    dynamic; <a href="http://www.spring1.com/link.asp">http://www.spring1.com/link.asp</a>?id=100408
    www domain; at <a href="www.wideweb.com">www.wideweb.com</a>
    single no subdomain; <a href="simple.com">simple.com</a>';
“另一个1体
规则
保护
子域
动态id=100408
www域;在
单个无子域;';
结果:除
http://www.spring1.com/link.asp?id=100408

正则表达式中缺少了什么使其起作用?

$bodyString=>
$bodyString = '
    another 1 body
    reg http://www.regularurl.com/home
    secure https://facebook.com/anothergreat.
    a subdomain http://info.craig.org/
    dynamic; http://www.spring1.com/link.asp?id=100408
    www domain; at www.wideweb.com
    single no subdomain; simple.com';

$regex = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@'; 
$converted_string = preg_replace($regex, '<a href="$0">$0</a>', $bodyString);
echo $converted_string;
另1个机构 规则http://www.regularurl.com/home 保护https://facebook.com/anothergreat. 子域http://info.craig.org/ 动态http://www.spring1.com/link.asp?id=100408 www域;www.wideweb.com 单个无子域;simple.com",; $regex='@(http)?(s)?(:/)?([a-zA-Z])([-\w]+\)+([^\s\.]+[^\s]*)+[^,.\s]); $converted_string=preg_replace($regex,,$bodyString); echo$转换的字符串;

正则表达式解释 另1个机构 规则http://www.regularurl.com/home 保护https://facebook.com/anothergreat. 子域http://info.craig.org/ 动态http://www.spring1.com/link.asp?id=100408 www域;www.wideweb.com 单个无子域;simple.com",; $regex='@(http)?(s)?(:/)?([a-zA-Z])([-\w]+\)+([^\s\.]+[^\s]*)+[^,.\s]); $converted_string=preg_replace($regex,,$bodyString); echo$转换的字符串;

正则表达式解释

[-\w@:%+.\\\\\\\\\\\?&/={2256}\.[a-z]{2,4}\b[^\s]*

[^\s]*
将向url添加任何非空格字符。当有空格时,它不是URL的一部分。简单易用

工作url

[-\w@:%+.\\~\\\\?&/={2256}\.[a-z]{2,4}\b[^\s]*

[^\s]*
将向url添加任何非空格字符。当有空格时,它不是URL的一部分。简单易用


工作url

基于@WiktorStribiżew的评论,您可以尝试以下内容:

[^\s]{2,256}\.[a-z]{2,4}\b(?:[?/][^\s]*)*

注意-虽然到目前为止已经有两个答案,但使用
[^\s]

解释-
[^\s]{2256}
匹配2到256个字符,这是
https://facebook
https://www.randomdomain
零件,
\。
匹配之后的点,
[a-z]{2,4}
是域扩展,例如:
com
in

\b
是单词的边界,
(?:[?/][^\s]*)*
是一个非捕获组,它匹配斜杠
/
或问号
和更多url,所有这些都可以重复零次或多次,表示url的子页面


为了更好地理解正则表达式语法,您应该基于@WiktorStribiżew的评论,尝试以下方法:

[^\s]{2,256}\.[a-z]{2,4}\b(?:[?/][^\s]*)*

注意-虽然到目前为止已经有两个答案,但使用
[^\s]

解释-
[^\s]{2256}
匹配2到256个字符,这是
https://facebook
https://www.randomdomain
零件,
\。
匹配之后的点,
[a-z]{2,4}
是域扩展,例如:
com
in

\b
是单词的边界,
(?:[?/][^\s]*)*
是一个非捕获组,它匹配斜杠
/
或问号
和更多url,所有这些都可以重复零次或多次,表示url的子页面


为了更好地理解正则表达式语法,您应该

也许。我认为如果您能像问题中那样编写这么多正则表达式,您可以自己找到解决方案。在从internet复制粘贴代码之前,请确保从您的角度进行尝试,并发布您的研究结果。也许吧。我认为,如果您能像问题中那样编写这么多正则表达式,您可以自己找到解决方案。在从internet复制粘贴代码之前,请确保从您的终端尝试并发布您的研究结果。