preg_replace没有按照我想要的方式工作


preg_replace not working as i want it to

我正在尝试使用 preg_replace() 函数操作文本,代码是不言自明的:

$fff = "12345678910";
echo $fff . "<br>";
$last = substr($fff,-5);
echo $last . "<br><br>";
$replace = "...";
$final = preg_replace($last,$replace,$fff);
echo $final;

因此,如果您不明白,我想将$fff缩短 5 个字符并将其替换为 ...然后将完整的单词存储到$final中,以便以后使用。但是我收到此错误:

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash in C:'htdocs'test.php on line 11

第 11 行:

$final = preg_replace($last,$replace,$fff);

感谢帮助。

为什么要使用preg_replace它可以完成

echo str_replace($last,$replace,$fff);

preg_replace使用正则表达式。您可能正在寻找str_replace:

str_replace($last, $replace, $fff);