b00ker_b0y Posted July 19, 2007 Share Posted July 19, 2007 Hello people! in need of some help as i just cant get this thing to work! I have a string i.e. Scotland-UKs-heart-100-attack-hotspot-18216660 i need to remove the last bit of the string which is an id. so realy need to start from the back, then keep everything till its the dash, then remove everything before the dash. At the moment i have this: $page_name = preg_replace('/[^0-9]/','',$page_name); this just removes everything except numbers from the string but then leaves with "10018216660" and it cant have the random 100 number in the middle to be included! any ideas on where or what to look for in solve this? Thanks for your time! Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/ Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 <?php $string = "Scotland-UKs-heart-100-attack-hotspot-18216660"; $pieces = explode("-", $string); $id = $pieces[6]; echo $id; ?> Simple Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302199 Share on other sites More sharing options...
b00ker_b0y Posted July 19, 2007 Author Share Posted July 19, 2007 lol awesom buddy! cheers for your mega quick responce lol as if your stalking the forums ready to help! lol cheers again! Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302201 Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 Yeah, got the PHP Help forum on auto refresh Good luck with the rest of your script, and you're welcome! Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302202 Share on other sites More sharing options...
b00ker_b0y Posted July 19, 2007 Author Share Posted July 19, 2007 arh, i am not sure if that will hold up! as that seems to be counting the the "dashs -"? so say if the string is "Scotland-UKs-heart-100-attack-hotspot-200-18216660" the its gonna pick up 200? no matter how long the string might be i need it to take the last id? Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302204 Share on other sites More sharing options...
chigley Posted July 19, 2007 Share Posted July 19, 2007 <?php $string = "Scotland-UKs-heart-100-attack-hotspot-18216660"; $pieces = explode("-", $string); $id = $pieces[(count($pieces) - 1)]; ?> Takes the last section every time Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302207 Share on other sites More sharing options...
DeadEvil Posted July 19, 2007 Share Posted July 19, 2007 you can use sub_str function.... $string = "Scotland-UKs-heart-100-attack-hotspot-18216660"; $id = sub_str($string,-8,0); Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302208 Share on other sites More sharing options...
matto Posted July 19, 2007 Share Posted July 19, 2007 I think you maybe looking for something like this: <?php $string = "Scotland-UKs-heart-100-attack-hotspot-18216660"; $pieces = explode("-", $string); echo end($pieces); ?> Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302217 Share on other sites More sharing options...
b00ker_b0y Posted July 19, 2007 Author Share Posted July 19, 2007 cheers veru much boys and girls! just went to lunch and came back with 3 ways to solve the problem! thanks to you all! Link to comment https://forums.phpfreaks.com/topic/60748-disecting-a-string-arhh/#findComment-302222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.