在线求助php正则表达式写法。
以下文本中出现的所有url想换成超链接形式。
“文字www.baidu.com文字
www.html.com
文字http://www.baidu.com
https://www.google.com”
代码如下:
public function make_clickable($str) {
$str = preg_replace('`([^"='>])((http://|https://|ftp://|ftps://|www.)[^s$2', $str);
return $str;
}
问题:
当文本为www.baidu.com时,超链接变成http://localhost/www.baidu.com
www前没有http或https的情况下,想替换成http://www.baidu.com的格式,再替换成超链接,
跪求正则表达式写法。
在此基础上改了下
$s = '“文字www.baidu.com文字www.html.com文字http://www.baidu.comhttps://www.google.com”';echo make_clickable($s);function make_clickable($str) { $str = preg_replace('`([^"='>])((http://|https://|ftp://|ftps://|www.)[^s< ]+[^s<.)])`ie',"_link('\1','\2')" , $str); return $str;}function _link($text,$link){ if(explode('.',$link)[0]=='www'){ $link='http://'.$link; } return $text.'<a href="'.$link.'" target="_blank" rel="nofollow">'.$link.'</a>';}非常有用。感谢
class Convert
{
/*
* URLを自?でリンク形式に
*/
public function make_clickable($str)
{
$str = preg_replace('`([^"='])((http://|https://|ftp://|ftps://|www.)[^s return $str;
}
private static function _link($text, $link)
{
if (explode('.', $link)[0] == 'www') {
$link = 'http://' . $link;
}
return $text . '' . $link . '';
}
}
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号