denniso Posted February 9, 2007 Share Posted February 9, 2007 Hi! I'm trying to modify an URL string with preg_replace in my SMF board ($buffer contains all the HTML code for the web page) There is some code for similar purposes already: $buffer = preg_replace('/"' . preg_quote($scripturl, '/') . '\?((?:board|topic)=[^#"]+?)(#[^"]*?)?"/e', "'\"' . \$scripturl . '/' . strtr('\$1', '&;=', '//,') . '.html\$2\"'", $buffer); It will replace all occurences of the URL http://chaimai.com/index.php?board=2.0 with http://chaimai.com/index.php/board,2.0.html and http://chaimai.com/index.php?topic=1666.0 with http://chaimai.com/index.php/topic,1666.0.html and http://chaimai.com/index.php?topic=1281.msg5110#msg5110 with http://chaimai.com/index.php/topic,1281.msg5110.html#msg In the example above $scripturl is set to "http://chaimai.com/index.php" I need to modify it (or make several calls to preg_replace) so it will Replace http://chaimai.com/index.php?board=2.0 with http://chaimai.com/thailand/forum/2/0.html and http://chaimai.com/index.php?topic=1654.0#new with http://chaimai.com/thailand/topic/1654/0.html#new and http://chaimai.com/index.php?action=dictionary;sa=details;id=83239 with http://chaimai.com/thai/lexikon/83239.html I know it's a lot to ask but I will really be greatful to any help in the right direction ... Thank You! Regards Dennis Quote Link to comment Share on other sites More sharing options...
effigy Posted February 9, 2007 Share Posted February 9, 2007 Try a callback: <pre> <?php $urls = array( '"http://chaimai.com/index.php?board=2.0"', '"http://chaimai.com/index.php?topic=1654.0#new"', '"http://chaimai.com/index.php?action=dictionary;sa=details;id=83239"' ); $scripturl = 'http://chaimai.com/index.php'; function fix_urls ($matches) { print_r($matches); list($url, $type) = $matches; switch ($type) { case 'board': // Regex... break; // ...and others. } }; foreach ($urls as $url) { echo $url, ' => '; $url = preg_replace_callback('/' . preg_quote($scripturl, '/') . '\?(board|topic|action)[^"]+/', 'fix_urls', $url); echo $url, '<br>'; } ?> </pre> Quote Link to comment Share on other sites More sharing options...
denniso Posted February 11, 2007 Author Share Posted February 11, 2007 Thank You @effigy !!! It looks like a nice solution. I will use it !!! I think it was very kind of you to help me and share your experience! Thanks! Dennis Quote Link to comment 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.