Jump to content

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.