1

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?

1
  • 2
    Why do you have .html inside the character group? And why do you negate the group with ^ at the beginning? Commented Nov 24, 2016 at 0:10

1 Answer 1

5

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);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

/-t([0-9]+)\.html/ just in case they have a URL like mysite.com/some-link-here-t90-t123.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.