Jump to content

How to get part of a string?


dmccabe

Recommended Posts

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.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136365-how-to-get-part-of-a-string/
Share on other sites

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.