Aliza Aliza - 16 days ago 5
PHP Question

preg_replace value dynamically php

I want to change these links dynamically

from

mysite.com/some-link-here-t123.html
mysite.com/some-link-here-t456.html
mysite.com/some-link-here-t789.html


to

mysite.com/some-link-here-tid123.html
mysite.com/some-link-here-tid456.html
mysite.com/some-link-here-tid789.html


I tried using below

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


but its messing up all the other words starts from
-t
like
-table
becomes
-tidable

how to fix this?

Answer

You need to limit your regex to only match numbers after the '-t' Like so:

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