irkevin Posted March 20, 2009 Share Posted March 20, 2009 Hi I have a code that will convert my Dynamic urls to static.. I've already used mod_rewrite which is ok but now have to use php to convert them according to the rewrited one. However with php, instead of having this: http://www.mu-anime.com/page/OngoingAnime/ I'm having this: http://www.mu-anime.com/index/page/OngoingAnime/ The 'index' is to much there and i dont have a clue how to remove it. Below are the relevant codes init.php define('BASE_URL', 'http://www.mu-anime.com/'); // this you must use as an absolute path to the application root, ex: /folder1/folder2/ if (isset($_SERVER['PATH_INFO'])) { $url = substr($_SERVER['PATH_INFO'], 1); $urlParts = explode('/', $url); if ($urlParts[count($urlParts) - 1] == '') array_pop($urlParts); $urlPartsCount = count($urlParts); if ($urlPartsCount % 2 != 0) { $urlPartsCount++; } for ($i = 0; $i < $urlPartsCount; $i += 2) { $_GET[$urlParts[$i]] = $urlParts[$i + 1]; } } $urlPatterns = array( '~'.preg_quote(BASE_URL).'([^\.]+[0-9a-zA-Z]+).php(\?([0-9a-zA-Z]+[^#"\']*))?~i', ); ob_start(); deinit.php function urlRewriteCallback($match) { $extra = ''; if ($match[3]) { $params = explode('&', $match[3]); if ($params[0] == '') array_shift($params); foreach ($params as $param) { $paramEx = explode('=', $param); $extra .= $paramEx[0].'/'.$paramEx[1].'/'; } } return BASE_URL.$match[1].'/'.$extra; } $pageContents = ob_get_contents(); ob_end_clean(); echo preg_replace_callback($urlPatterns, 'urlRewriteCallback', $pageContents); Someone help me with this please Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/ Share on other sites More sharing options...
.josh Posted March 20, 2009 Share Posted March 20, 2009 if you want to remove index from it, just do str_replace('/index/','/',$string); Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/#findComment-789699 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Author Share Posted March 20, 2009 thnx for the reply but there is no variable $string in the script, so which variable should i pass instead of $string? Any Idea? Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/#findComment-789725 Share on other sites More sharing options...
.josh Posted March 20, 2009 Share Posted March 20, 2009 $string is a generic variable, used to show the 3rd argument in str_replace. $subject is also a common one. In other words, you need to put your own variable in there. Whatever variable that has your url you want changed. Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/#findComment-789773 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Author Share Posted March 20, 2009 i've been able to remove /page/ by changing this: $extra .= $paramEx[0].'/'.$paramEx[1].'/'; to this: $extra .= $paramEx[1].'/'; but still, the index is here.. dont have a clue how to remove it.. i should use an if statement maybe but dont know how Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/#findComment-789779 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Author Share Posted March 20, 2009 i noticed something.. if my link is like this -> index.php?action=register when i change return BASE_URL.$match[1].'/'.$extra; to return BASE_URL.$extra; it worked welll.... but if my link is like this http://www.mysite.com/page.php it doesn't work.. i get only the domain name of my site without the page.php Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/#findComment-789796 Share on other sites More sharing options...
irkevin Posted March 20, 2009 Author Share Posted March 20, 2009 Fixed here's how i did it.. again it was a str_replace on deinit.php: <?php function urlRewriteCallback($match) { $extra = ''; if ($match[3]) { $params = explode('&', $match[3]); if ($params[0] == '') array_shift($params); foreach ($params as $param) { $paramEx = explode('=', $param); $extra .= $paramEx[1].'/'; } } return BASE_URL.$match[1].'/'.$extra; } $pageContents = ob_get_contents(); ob_end_clean(); $pregit = preg_replace_callback($urlPatterns, 'urlRewriteCallback', $pageContents); $preg = str_replace('index/','',$pregit); echo $preg; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150360-solved-help-with-replace-dynamic-url-into-static-url/#findComment-789809 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.