Php preg_动态替换值

Php preg_动态替换值,php,Php,我想动态更改这些链接 从 到 我试着在下面使用 <?php $string2 = preg_replace('/-t([^0-9.html]*)/','-tid$1',$string); ?> 但是它把所有其他单词都弄乱了,从-t开始,就像-table变成了-tidable 如何解决此问题?您需要将正则表达式限制为仅匹配“-t”后面的数字,如: <?php $string2 = preg_replace('/-t([0-9]+)/','-tid$1',$string)

我想动态更改这些链接

我试着在下面使用

<?php 
$string2 = preg_replace('/-t([^0-9.html]*)/','-tid$1',$string);
?>

但是它把所有其他单词都弄乱了,从
-t
开始,就像
-table
变成了
-tidable

如何解决此问题?

您需要将正则表达式限制为仅匹配“-t”后面的数字,如:

<?php 
$string2 = preg_replace('/-t([0-9]+)/','-tid$1',$string);
?>

您需要将正则表达式限制为只匹配“-t”后面的数字,如下所示:

<?php 
$string2 = preg_replace('/-t([0-9]+)/','-tid$1',$string);
?>


为什么字符组中有
.html
?为什么在开始时用
^
否定组?为什么字符组中有
.html
?为什么你一开始就用
^
来否定这个组?/-t([0-9]+)\.html/以防万一他们有像mysite.com/some-link-here-t90-t123.html/-t([0-9]+)\.html/以防万一他们有像mysite.com/some-link-here-t90-t123.html这样的URL
<?php 
$string2 = preg_replace('/-t([0-9]+)/','-tid$1',$string);
?>