split text into variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

split text into variables

Post by JKM »

Hi there,

I'm having a string like this 'text text textblabla (<year>)' and I want to split it into $text, $year - but I'm not really sure how to do it.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: split text into variables

Post by JNettles »

You can split a string of text into an array - you need to establish a consistent pattern to split by though. Example.....

Code: Select all

 
$string = "peter<~>lois<~>meg<~>chris<~>brian";
 
$split = explode("<~>", $string);
 
echo $split[0]; //outputs 'peter'
 
If you need something more complex than this I'd look into XML.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: split text into variables

Post by JKM »

Sorry, that won't do the trick - () can occur inside the text, so it should only fetch the text before (<year>) AND fetch the year without ().
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: split text into variables

Post by JNettles »

How about you show the string you're actually wanting to split? Explaining your problem fully is the first key to getting help.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: split text into variables

Post by JKM »

It's IMDB titles,
- Pulp Fiction (1994) | $title = 'Pulp Fiction', $year = 1994
- 2001: A Space Odyssey (1968) | $title = '2001: A Space Odyssey', $year = 1968
- Lock, Stock and Two Smoking Barrels (1998) | $title = 'Lock, Stock and Two Smoking Barrels', $year = 1998
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: split text into variables

Post by JNettles »

You can still use the example I posted - just use ( to split.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: split text into variables

Post by JKM »

What about for example (500) Days of Summer? :)
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: split text into variables

Post by Grizzzzzzzzzz »

Regular Expressions :wink:
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: split text into variables

Post by lshaw »

preg_split("regex here",$srting);

You need to look into regular expressions and find out all the time your code needs to split. Someone please correct me if I have got the wrong funtion :lol:
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: split text into variables

Post by JNettles »

Personal opinion but I tend to avoid regular expressions like death or marriage. They're slow, a pain to write, and can be easily broken the moment an odd string shows up. Again, my personal opinion but you'd probably be better off writing your own parsing routine using PHP's string functions if you can't get explode to work.
JKM
Forum Contributor
Posts: 221
Joined: Tue Jun 17, 2008 8:12 pm

Re: split text into variables

Post by JKM »

I found another way to do it - how do I pick out only the year from a string? 'blablabla YYYY sadasd'
Post Reply