dtdetu Posted December 16, 2008 Share Posted December 16, 2008 hello can somebody help me how i can change page?id=1 to page value of id changes everytime, like id=2 id=4 etc thanks Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/ Share on other sites More sharing options...
rhodesa Posted December 16, 2008 Share Posted December 16, 2008 can you elaborate on what you are trying to accomplish? is the first part in a string? are you trying to just remove the id attribute or strip everything after the question mark? Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716656 Share on other sites More sharing options...
dtdetu Posted December 16, 2008 Author Share Posted December 16, 2008 i am tryign to get the page name of the page but i want to get the page name alone like this page is index.php?id=12 i want to get index , can u also give me the code for get page name thanks Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716658 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 dtdetu, I mean no offense when I say this, but I would strongly recommend taking some time to learn regex basics. It isn't hard to learn per say.. a little intimidating at first, but once you get past the basics, you realise how useful regex can be. Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716660 Share on other sites More sharing options...
rhodesa Posted December 16, 2008 Share Posted December 16, 2008 http://us3.php.net/manual/en/function.parse-url.php Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716666 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 And if all you want is the breakdown of the url, you can use parse_url instead: example: $str = 'index.php?id=12'; $parse = parse_url($str); echo '<pre>'.print_r($parse, true); Output: Array ( [path] => index.php [query] => id=12 ) Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716670 Share on other sites More sharing options...
dtdetu Posted December 16, 2008 Author Share Posted December 16, 2008 thank you all , there is too much to learn and not enough time thanks Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716674 Share on other sites More sharing options...
dtdetu Posted December 16, 2008 Author Share Posted December 16, 2008 i used this code and works , incase someone needs.. thanks $str = $_SERVER['REQUEST_URI']; $parse = parse_url($str); $parse[path]=str_replace('/','',$parse[path]); $parse[path]=str_replace('.php','',$parse[path]); return $parse[path]; Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716678 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 thank you all , there is too much to learn and not enough time thanks Poor excuse which can only last so long... No one has all the time to learn.. we make the time to do so. Otherwise, you are at the mercy of everyone else.. a pity really. Don't be lazy. Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716690 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 See, now that I see you post a code snippet, I can see you are making effort.. good for you. Here is my take on this: $str = 'index.php?id=12'; $parse = parse_url($str); preg_match('#^([^.]+)#', $parse['path'], $match); echo $match[1]; Output: index Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716693 Share on other sites More sharing options...
dtdetu Posted December 16, 2008 Author Share Posted December 16, 2008 thanks Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716695 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 Or if you wanted to use preg_replace instead: $str = $_SERVER['REQUEST_URI']; $parse = parse_url($str); $parse['path'] = preg_replace('#^/([^.]+).*#', "$1", $parse['path']); echo $parse['path']; Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716702 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 Actually, come to think of it, we can just get that first part even without the parse url bit: $str = $_SERVER['REQUEST_URI']; $str = preg_replace('#^/([^.]+).*#', "$1", $str); echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716710 Share on other sites More sharing options...
dtdetu Posted December 16, 2008 Author Share Posted December 16, 2008 yes but if we dont use parse url then it gets the ?id=2 part too and other $_GETs Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716712 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 yes but if we dont use parse url then it gets the ?id=2 part too and other $_GETs It should work.. here is the breakdown of the preg: '#^/([^.]+).*#', "$1", $str Basically, this is saying: At the start of the string ( noted by the ^ character), after the /, capture anything that is not a dot, and replace the entire thing with just what was captured.. so if I were to use '/someFile.php?id=7' to pretend it is the end results of $_SERVER['REQUEST_URI'], I plug this into the last snippet example like so: $str = '/someFile.php?id=7'; $str = preg_replace('#^/([^.]+).*#', "$1", $str); echo $str; The regex will only use what it captured.. so in this case, the initial /, and everything (including and after) the dot character is replaced with what is inside those brackets.. so in the above case, the result would be someFile Try it out. Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716725 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 You know what? Even easier (now that I think about it.. [man I must be brain dead today]), is to use pathinfo like so: $str = $_SERVER['REQUEST_URI']; $x = pathinfo($str); echo $x[filename]; So let's pretend $_SERVER['REQUEST_URI'] = /someFile.php?id=7, once again, the end result will be someFile I think this snippet is the best way to go. Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716757 Share on other sites More sharing options...
dtdetu Posted December 16, 2008 Author Share Posted December 16, 2008 hehe thank you:) dont think more about this its ok:) Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716759 Share on other sites More sharing options...
nrg_alpha Posted December 16, 2008 Share Posted December 16, 2008 ok done deal. Quote Link to comment https://forums.phpfreaks.com/topic/137193-solved-easy-replace/#findComment-716761 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.