Regex preg_的正则表达式替换为一定数量的字符

Regex preg_的正则表达式替换为一定数量的字符,regex,Regex,我有一个类似这样的变量: text1 http://www.server.com/10characters text2 http://www.server.com/10characters text3 我想把所有的都替换掉。”http://www.server.com/10characters链接“单击”,但http://www.server.com/“是一个必须出现的常量,“10个字符”始终是任意10个字符(不少于,不多于) 例如。 替换 到 text1 text2 文本3http://ww

我有一个类似这样的变量:

text1 http://www.server.com/10characters text2 http://www.server.com/10characters text3
我想把所有的都替换掉。”http://www.server.com/10characters链接“单击”,但http://www.server.com/“是一个必须出现的常量,“10个字符”始终是任意10个字符(不少于,不多于)

例如。 替换

text1 text2
文本3http://www.otherserver.com/xt92s5sfa2 文本3

我不知道怎么做:/I我尝试了几种方法,但没有取得好的效果。

如果域后总是10个字符,则不需要preg_替换:

$url1 = substr($url,0,35); //length of http://www.server.com/10characters is 35 chars
echo "text1 <a href=\"$url1\">click</a><br>";
/// etc
$url1=substr($url,0,35)//长度http://www.server.com/10characters 是35个字符
回显“text1
”; ///等
我想

preg_replace("http://www\.server\.com/[0-9a-zA-Z]{10}", " Click!", $myLink)
应该有效。

$str='text1http://www.server.com/d19d2aj53f 文本2http://www.server.com/a49ds5j3ax 文本3http://www.otherserver.com/a49ds5j3ax 文本3';
$str = 'text1 http://www.server.com/d19d2aj53f text2 http://www.server.com/a49ds5j3ax text3 http://www.otherserver.com/a49ds5j3ax text3';

echo preg_replace('~http://www\.server\.com/.{10}~i', '<a href="$0">click</a>', $str);
echo preg_更换('~http://www\.server\.com/{10}~i',''$str);
在模式中,
是“任意字符”,因此
{10}
表示任意十个字符

在替换中,
$0
表示整个模式匹配的内容(在本例中为完整URL)


这是其中的一部分。

不要忘记并逃避你的
s。此外,这些示例似乎表明,这十个字符不限于数字。我可以解释得不好,但我希望它被替换为这样:但它必须找到并替换链接的所有http:/www.server.com/10字符,而不是一个。在文本中,您甚至可以有3个或更多。@user1604488您可以在循环中运行它;)
preg_replace("http://www\.server\.com/[0-9a-zA-Z]{10}", " Click!", $myLink)
$str = 'text1 http://www.server.com/d19d2aj53f text2 http://www.server.com/a49ds5j3ax text3 http://www.otherserver.com/a49ds5j3ax text3';

echo preg_replace('~http://www\.server\.com/.{10}~i', '<a href="$0">click</a>', $str);