split text into variables
Moderator: General Moderators
split text into variables
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.
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.
Re: split text into variables
You can split a string of text into an array - you need to establish a consistent pattern to split by though. Example.....
If you need something more complex than this I'd look into XML.
Code: Select all
$string = "peter<~>lois<~>meg<~>chris<~>brian";
$split = explode("<~>", $string);
echo $split[0]; //outputs 'peter'
Re: split text into variables
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 ().
Re: split text into variables
How about you show the string you're actually wanting to split? Explaining your problem fully is the first key to getting help.
Re: split text into variables
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
- 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
Re: split text into variables
You can still use the example I posted - just use ( to split.
Re: split text into variables
What about for example (500) Days of Summer? 
- Grizzzzzzzzzz
- Forum Contributor
- Posts: 125
- Joined: Wed Sep 02, 2009 8:51 am
Re: split text into variables
Regular Expressions 
Re: split text into variables
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
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
Re: split text into variables
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.
Re: split text into variables
I found another way to do it - how do I pick out only the year from a string? 'blablabla YYYY sadasd'