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! Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment 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! Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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); Quote Link to comment 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); ?> Quote Link to comment 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! Quote Link to comment 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.