basic regex question (PHP)

Soldato
Joined
3 Oct 2009
Posts
2,719
I have a redirect plugin for my wordpress blog that allows rules based on matching regular expressions, which i do not understand at all.

I need a pattern that matches something like
/toy-models/tonka-blue/

and changes it to
/toys/tonka/

The base urls are always "/toymodels/" followed by one word, and a hyphen, and everything after the hyphen can be discarded.

So it's to search for the pattern:
"/toymodels/"<one word for a toy type><a hyphen><some stuff to discard>
and replace with
"/toys/"<the toy type found above>"/"

Any help?
 
PHP:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$pos = strpos($url, "-");

$newUrl = substr_replace($url, '', $pos);

header("Location: ". $newUrl);

Works providing there is only one dash...
 
There'll always be two (notice there's one in the "toy-models" and it's the bit after that I;m looking for the hyphen.

I should have been more clear though - I am using a wordpress plugin to set up custom redirects for a subset of the links on my blog. It needs regex input, I'm not directly accessing the server.

After many hours building up a huge headache, I think I've figured out something that works:

The string to Find: (/toy-models/)(\w*)-(\w*)
The string to Replace: /toys/$2/
 
Back
Top Bottom