PHP- Using Preg_replace To Strip Date From URL

Image frame
PHP- Using Preg_replace To Strip Date From URL
Maruf
Sep 18th, 2009
Maruf scribbled this post.

This is a very specific problem I was faced with a few days ago, so I doubt many people will find it directly useful. However, the solution to this problem can be used to resolve a load of tricky regular expression issues.

The Scenerio

A client recently changed permalink structures for his entire website. For example, all his blog links were as follows:
http://www.example.com/blog/2009/12/09/example-blog-post/

For various reasons, the webmaster wanted to change the structure to:
http://www.example.com/blog/example-blog-post/ (stripping out the date)

The problem was, almost all the pages were internally linking to one another with the old URL structure, so the actual content of the page needed to be updated. In fact, in most cases, there were 4 or 5 links that needed to be updated on each page.

The webmaster didn’t want to manually modify each page because it would obviously take forever and a day. At first I thought it could be done with an MySQL query (page content is stored in a DB), but it actually can’t, so I had to create a PHP script to change all the links. The key function to enable this being preg_replace.

The Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
// define the pattern you want to replace. The "d" is for "digit", you can use "w" for alpanumeric
$pattern = '/example.co.uk\/blog\/(\d+)\/(\d+)\/(\d+)\//';
 
//define the placement
$replacement = 'example.co.uk/blog/';
 
$string = "this is the string we want to replace <a href="http://www.example.co.uk/blog/2009/12/09/example-blog-post">link</a>. More writing here <a href="http://www.example.co.uk/blog/2009/12/09/example-blog-post">link</a>";
 
$string = preg_replace($pattern, $replacement, $string);
echo $string;
 
//the output will be as follows:
this is the string we want to replace <a href="http://www.example.co.uk/blog/example-blog-post">link</a>. More writing here <a href="http://www.example.co.uk/blog/example-blog-post">link</a>

And there you have it. Because it’s a regular expression function, the pattern is replaced EVERYTIME it is matched. The example above was the core function in the script I wrote. I actually used it in a while loop, to update a specific field in every row in a specific table, so each page was updated.

If anyone actually uses this example, please let me know how you get on with it.


Filed away: PHP

feel free to leave a scribble

Name:
Email:
gravatar
What an image next to your comments?
visit gravatar.com
Message:
want a website, do ya?

latest projects

Image frame
RG Active
view site- RG Active
Image frame
Property Investment Project
view site- Property Investment Project

latest scribbles

latest comments

zurin Gravatar frame
zurin said
hi, i’m having problem with this function. it works fine...
Karlos Gravatar frame
Karlos said
Awesome awesome awesome :) Just started re-designing...
satheesh Gravatar frame
satheesh said
It solved my problem. Thank you. Is it possible to get the...

blog categories

Get a free quote