tags:

views:

43

answers:

2

i have a url: /events/details/16-righteous-turkeys.html

that i need to change to /turkeys

i have no idea how to go about doing this...and would really appreciate the help.

+1  A: 

The first thing you need to do is specify your problem more clearly.

What you've posted can be achieved simply by assigning the value "/turkeys" to your variable (maybe you want to use an 'if' statement to check it previously contains "/events/details/16-righteous-turkeys.html").

So you need to specify (in English first) how you figured out what the new value should be. An example that may fit what you've posted:

"I need to take my original path and, ignoring the directories, look at the filename on the end. From that, I need to extract everything between the last hyphen and the .html extension on the end. I then need to replace the entire thing with what I've extracted ("turkeys" in this case)".

ishnid
sorry, i thought it was more clear than i guess it was. the url is being generated by a plugin for a system i am using within a cms. it is giving me one small line to plug in a regex to replace it. i need to replace everything after the first / with the word turkey.
liz
Yes, but why "turkey". Is it always "turkey" (unlikely)? Or what's between the last hyphen and the .html? Maybe the extension isn't always .html: should this work with .htm, .jpg etc too? Is there always a hyphen, or can it happen that the "16-righteous-turkeys-" part isn't there, and your link is just "/events/details/turkeys.html"? My point was that there are many ways to interpret exactly what you're trying to do based on just one specific example.
ishnid
lol yes in this case it was a simple trade one for one... never to be repeated... i found another way to do it, thanks for your help.
liz
A: 

If you need to always extract the last word between the hyphen and .html the following should do it:

$string = '/events/details/16-righteous-turkeys.html';
$pattern = '/^.*\-(\w+)\.html$/i';
$replacement = '/${1}';
echo preg_replace($pattern, $replacement, $string);
klausbyskov