如何从字符串中替换“✦”字符的所有域名?

时间:2017-07-22 09:27:23

标签: php

我得到的最多:

$name = str_replace("*.*", "✦", $name);

例如:

$name = "Hello | google.com"

将被替换为:

$name = "Hello | ✦"

2 个答案:

答案 0 :(得分:0)

尝试使用regex

<?php 
preg_replace('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&@#\/%=~_|$?!:,.]*[A-Z0-9+&@#\/%=~_|$]/i','✦',$name);
?>

答案 1 :(得分:0)

您可以使用preg_replace()进行模式匹配替换。

$name = "Hello | google.com";
//$name = preg_replace("/[a-zA-Z]+\.[a-zA-Z]+/", "✦", $name);
 $name = preg_replace("/((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z0-9\&\.\/\?\:@\-_=#])+/", "✦", $name);

echo $name;