Movable Type: remove part of URL using regex replace

116 Views Asked by At

I want to remove part uf url using regex_replace I tried below but its not working right. I am trying to remove "index1.php"

Code input:

http://www.myblog.com/blog/content/fruit/index1.php

my code

<mt:getvar name="permalink" regex_replace="/index1\.(php)$/","">

Code output:

http://www.myblog.com/blog/content/fruit/index1.php

But it should be

http://www.myblog.com/blog/content/fruit/
2

There are 2 best solutions below

0
hjpotter92 On

Try the following pattern:

<mt:getvar name="permalink" regex_replace="/\/[^\/]+$\/","">

or, try using a capture group:

<mt:getvar name="permalink" regex_replace="/(.*\/)index1[.]php$\/","$1">
0
Dan Wolfgang On

For the example you posted you don't need to use regex_replace, you can just use replace:

<mt:GetVar name="permalink" replace="index1.php","">

It's not clear if you might be planning to use regex_replace to actually do a regular expression match (perhaps the number varies or something), but if not, replace is easier and faster.