dsp77 Posted August 31, 2011 Share Posted August 31, 2011 Hello, i have this type of link: http://www.example.com/cat/name-of-article/5054673 and i want to transform it into: http://www.example.com/cat/name-of-article/full_screen/5054673 i think it can be done with explode or str_replace but i had no luck. Any advise? Thank you Link to comment https://forums.phpfreaks.com/topic/246093-add-into-a-string-other-string/ Share on other sites More sharing options...
dsp77 Posted August 31, 2011 Author Share Posted August 31, 2011 it works and is fast to:) foreach(array_slice(explode("/", $a['url']), -2, 1) as $link){ $dlink = 'http://example.com/cat/'.$link.'/full_screen/'.$a['id']; } Link to comment https://forums.phpfreaks.com/topic/246093-add-into-a-string-other-string/#findComment-1263837 Share on other sites More sharing options...
Adam Posted August 31, 2011 Share Posted August 31, 2011 All you need to do is work out the position of the far right "/" using strrpos, and then insert text between that point using substr: $str = 'http://www.example.com/cat/name-of-article/5054673'; $pos = strrpos(rtrim($str, '/'), '/'); $str = substr($str, 0, $pos) . '/full_screen' . substr($str, $pos); echo $str; Notice the use of rtrim in-case there's a trailing slash. Link to comment https://forums.phpfreaks.com/topic/246093-add-into-a-string-other-string/#findComment-1263839 Share on other sites More sharing options...
dsp77 Posted August 31, 2011 Author Share Posted August 31, 2011 thanks man Link to comment https://forums.phpfreaks.com/topic/246093-add-into-a-string-other-string/#findComment-1263850 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.