preg_split problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    preg_split problem

    L.S.

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

    $data='one|two| three|four\nfiv e|six|seven|eig ht'; //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",$dat a); //should give me an array $sentence
    $word=preg_spli t("|",$senten ce[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.


  • Peter

    #2
    Re: preg_split problem

    YES!!! That appears to do the trick. Thank you.

    Peter.


    Comment

    • matty

      #3
      Re: preg_split problem

      Peter wrote:
      [color=blue]
      > L.S.
      >
      > I am trying to cut a string into substrings. The string at hand looks
      > something like this:
      >
      > $data='one|two| three|four\nfiv e|six|seven|eig ht'; //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",$dat a); //should give me an array $sentence
      > $word=preg_spli t("|",$senten ce[0]); //should give me an array $word
      >[/color]

      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

      Comment

      Working...