dmccabe Posted December 10, 2008 Share Posted December 10, 2008 Ok lets say I a variable containing a string like this: $var = "04 - blahblahblahblah" How can I grab the "blahblahblahblah" bit ? in to another variable? It wont always be the same length and it wont always be prefixed with 04, but will always have the " - " in there. Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/ Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 $x = explode(" - ",$var); echo $x[0]; edit: oops. didn't see the "not always prefixed with digit" $x = explode("- ",$var); echo $x[0]; or $var = "04 - blahblahblah"; preg_match("/-\s(\w*)/",$var,$match); echo $match[1]; Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711433 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 in Crayon Violent's example, $x[0] will be the 04, $x[1] should be the second half (the blahblahblahblah) Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711436 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 Yeah typo. Also I didn't see the "not always number prefix part." see edit above Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711437 Share on other sites More sharing options...
Brian W Posted December 10, 2008 Share Posted December 10, 2008 Crayon Violent, you are keen and thorough... but most everyone hates regular expressions. lol $x = explode("-",$var); echo trim($x[1]); Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711449 Share on other sites More sharing options...
dmccabe Posted December 10, 2008 Author Share Posted December 10, 2008 Thanks for the quick help Crayon. One thing I never noticed though. The lines I am trying to split up are like this: 04 - blahblahblahblah - thisthatandtheother So the explode function chops off the " - thisthatandtheother" part, but I need that to be all part of the same string, I just want to get separate it into 2 parts 1 = 04 2 = blahblahblahblah - thisthatandtheother Sorry my bad I should have been more specific in my original post Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711453 Share on other sites More sharing options...
bluesoul Posted December 10, 2008 Share Posted December 10, 2008 The arguments for explode are explode ( string $delimiter , string $string [, int $limit ] ) so you can do... $x = explode("-",$var,2); echo trim($x[1]); And get the result you're wanting Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711456 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 Crayon Violent, you are keen and thorough... but most everyone hates regular expressions. lol True that. I myself only started giving regex a serious effort about a month ago. It's scary and frustrating and addictive to all hell. And on that note... $var = "04 - blahblahblahblah - thisthatandtheother"; preg_match("/-\s(.*)/",$var,$match); echo $match[1]; That will work, even if your string were like 04 - blahblahblahblah - thisthatandtheother 04 - blahblahblahblah - thisthatandtheother - laksdklsdfj 04 - blahblahblahblah - thisthatandtheother - lksdfjklsjdf - lkasdfjlksfj - kasjdfljlj or hell, anything with an initial "- " - blkajsdlfkjslfskjdf - blkjasdflkj asdlkjfs laksjfdls - asldfkjasdf Quote Link to comment https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/#findComment-711476 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.