By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

preg_split problem

P: n/a
L.S.

I am trying to cut a string into substrings. The string at hand looks
something like this:

$data='one|two|three|four\nfive|six|seven|eight'; //observe the chr(10) in
the middle

I would like to have this split two times. Once to give me the two
'sentences' 'one|two|three|four' and 'five|six|seven|eight'. Then, each of
these sentences would be split into their repective words. Initially I
thought it would be simple:

$sentence=preg_split("\n",$data); //should give me an array $sentence
$word=preg_split("|",$sentence[0]); //should give me an array $word

Unfortunately this doesn't work and now I am caught up in a new world of
'regular expression functions' that doesn't seem to make any sense. Can you
please help me to get this split function to do what I need?

Thanks for your reply, Peter.
Jul 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
YES!!! That appears to do the trick. Thank you.

Peter.
Jul 16 '05 #2

P: n/a
Peter wrote:
L.S.

I am trying to cut a string into substrings. The string at hand looks
something like this:

$data='one|two|three|four\nfive|six|seven|eight'; //observe the chr(10) in
the middle

I would like to have this split two times. Once to give me the two
'sentences' 'one|two|three|four' and 'five|six|seven|eight'. Then, each of
these sentences would be split into their repective words. Initially I
thought it would be simple:

$sentence=preg_split("\n",$data); //should give me an array $sentence
$word=preg_split("|",$sentence[0]); //should give me an array $word


As pointed out elsewhere, it is better to use explode.

However, if you are doing things like this with pcre, you have to bear in mind
that if your expressions contain newlines, you have to use a multiline modifier.
Have a read and a play with it (read the manual stuff on modifiers)

Matt
Jul 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.