Home > Backend Development > PHP Tutorial > PHP 正则表达式,该如何解决

PHP 正则表达式,该如何解决

WBOY
Release: 2016-06-13 12:06:56
Original
898 people have browsed it

PHP 正则表达式
请问现在有字段$a=https://www.baidu.com
aaaaaa
https://www.163.com

请问如何使用正则表达式截取字段,使https://www.baidu.com 和https://www.163.com变为https://www.baidu.com 这种样子的超链接
------解决思路----------------------

<br />$a="https://www.baidu.com<br>aaaaaa<br>https://www.163.com<br>";<br /><br />$ret= preg_replace(<br />'/((news<br><font color='#FF8000'>------解决思路----------------------</font><br>telnet<br><font color='#FF8000'>------解决思路----------------------</font><br>nttp<br><font color='#FF8000'>------解决思路----------------------</font><br>file<br><font color='#FF8000'>------解决思路----------------------</font><br>http<br><font color='#FF8000'>------解决思路----------------------</font><br>ftp<br><font color='#FF8000'>------解决思路----------------------</font><br>https):\/\/)(([-A-Za-z0-9]+(\.[-A-Za-z0-9]+)*(\.[-A-Za-z]{2,5}))<br><font color='#FF8000'>------解决思路----------------------</font><br>([0-9]{1,3}(\.[0-9]{1,3}){3}))(:[0-9]*)?(\/[-A-Za-z0-9_\$\.\+\!\*\(\),;:@&=\?\/~\#\%]*)*/',<br />'<a href="\0">\0</a>',$a);<br /><br />echo $ret;<br />?>
Copy after login


url的正则可以换个更好的
------解决思路----------------------
<br />$a='https://www.baidu.com<br>aaaaaa<br>https://www.163.com<br>';<br />$new=preg_replace('/((https<br><font color='#FF8000'>------解决思路----------------------</font><br>http):\/\/[^<]+)/i',"<a href=\"$1\">$1</a>",$a);<br />echo $new;<br />/*<br /><a href="https://www.baidu.com">https://www.baidu.com</a><br>aaaaaa<br><a href="https://www.163.com">https://www.163.com</a><br><br />*/<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template