bertom Posted March 26, 2010 Share Posted March 26, 2010 Hello, I was hoping to find an answer here on this question: I need to convert some urls dynamically with php: http://www.domain.com/something-variable/12345/ into http://www.domain.com/something-variable/12345/?xx=yy the last two parts of the url are variable, and as you can see, the the url needs a variable string attached at the end. (always the same). I told you it was easy. I found out that this could be done through a preg_replace command, and I tried, but i have no idea how to fix this. I am not a developer you see. Any help is appreciated. Thanks, Bert Quote Link to comment https://forums.phpfreaks.com/topic/196587-very-simple-regex-question-for-someone-who-just-cant-code/ Share on other sites More sharing options...
cags Posted March 26, 2010 Share Posted March 26, 2010 Roughly speaking something like this... $output = preg_replace('#http://www\.domain\.com/([a-z0-9-]+)/([0-9]+)/#i', 'http://www.domain.com/$1/$2/?xx=yy', $input); Quote Link to comment https://forums.phpfreaks.com/topic/196587-very-simple-regex-question-for-someone-who-just-cant-code/#findComment-1032176 Share on other sites More sharing options...
bertom Posted March 26, 2010 Author Share Posted March 26, 2010 Thanks for the answer Cags, I'll give that a try. In your example, what would the #i, the $1 and $2 stand for? Just trying to understand. Quote Link to comment https://forums.phpfreaks.com/topic/196587-very-simple-regex-question-for-someone-who-just-cant-code/#findComment-1032189 Share on other sites More sharing options...
cags Posted March 26, 2010 Share Posted March 26, 2010 The i makes the pattern case insensitive. The $1 and $2 represent capture groups one and two. Basically it will insert in their places the values captured in the pattern between the corresponding set of brackets. So $1 will be the first directory in the URI and $2 it's child directory. Quote Link to comment https://forums.phpfreaks.com/topic/196587-very-simple-regex-question-for-someone-who-just-cant-code/#findComment-1032190 Share on other sites More sharing options...
bertom Posted March 26, 2010 Author Share Posted March 26, 2010 Well then this must be exactly what I need. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/196587-very-simple-regex-question-for-someone-who-just-cant-code/#findComment-1032191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.