kks_krishna Posted October 19, 2007 Share Posted October 19, 2007 HI, I have the following url : http://www.sitename.com/articles/2007/10/article-title-name/2 I want to get only the "article-title-name" from the entire url. How can I do that? some time the url will be "http://www.sitename.com/articles/2007/10/article-title-name/". it is first page of the article. Please help me. Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/ Share on other sites More sharing options...
enoyhs Posted October 19, 2007 Share Posted October 19, 2007 You can try this: <?php $text = "http://www.sitename.com/articles/2007/10/article-title-name/2"; $text = substr($text,0,strrpos($text,"/")); $text = substr($text,strrpos($text,"/")+1); ?> Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-373019 Share on other sites More sharing options...
kks_krishna Posted October 19, 2007 Author Share Posted October 19, 2007 Thanks, I will try it. Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-373105 Share on other sites More sharing options...
kks_krishna Posted October 22, 2007 Author Share Posted October 22, 2007 "http://www.sitename.com/articles/2007/10/article-title-name/2" value i have to take from the address bar of the browser. is there any way in PHP to do that? Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-375479 Share on other sites More sharing options...
GingerRobot Posted October 22, 2007 Share Posted October 22, 2007 I would explode by the slashes, and then use the relevant element of the array: <?php $str = 'http://www.sitename.com/articles/2007/10/article-title-name/2'; $bits = explode('/',$str); echo $bits[6];//the article name ?> Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-375489 Share on other sites More sharing options...
MadTechie Posted October 22, 2007 Share Posted October 22, 2007 i think he means <?php //Get the URI (from the address bar) $str = $_SERVER['REQUEST_URI']; //+GingerRobot code $bits = explode('/',$str); echo $bits[4];//the article name //(changed to 4 as REQUEST_URI skips the http://) ?> Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-375500 Share on other sites More sharing options...
rajivgonsalves Posted October 24, 2007 Share Posted October 24, 2007 You can use the following $str = 'http://www.sitename.com/articles/2007/10/article-title-name/2'; preg_match("#http://[\w\d\.\-]+/[\w\d\.\-]+/\d{4}/\d+/([\w\d\.\-]+)/\d*#",$str,$matches); print_r($matches[1]); Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-376929 Share on other sites More sharing options...
MadTechie Posted October 24, 2007 Share Posted October 24, 2007 If you was going to use RegEx, this would be simpler! <?php //Get the URI (from the address bar) #$str = $_SERVER['REQUEST_URI']; $str = 'http://www.sitename.com/articles/2007/10/article-title-name/2'; preg_match_all('%/([^/]*)%i', $str, $result, PREG_PATTERN_ORDER); $bits = $result[1]; echo $bits[5]; ?> Link to comment https://forums.phpfreaks.com/topic/73922-need-help-in-string-manipulation/#findComment-376944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.