How do you split a string using split()?

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

    How do you split a string using split()?

    I have this PHP statement which works fine and breaks down a string of
    lines delimited with a \r into an array.

    $arrList=split( "\r", $strList);

    But what I really want is to be able to tell it to split out only the
    strings that start with the letter "b".

    E.g. I thought this would work "b\W\r"

    I tried every regular expression but cant figure it out. I could use
    some other function combinations to achive this but I though if there
    is a way to do it with just spli()

    Any ideas?

  • geeker87@gmail.com

    #2
    Re: How do you split a string using split()?

    Your regular expression would probably look something like this:

    /^b(.+)\r/

    For a line that begins with the letter 'b' is followed by one or more
    'anything', ending with '\r'.

    You would split the file into multiple lines with split(), then pass
    each line to preg_match as $inputLine.

    So you could use:

    $numParts = preg_match($exp ression,$inputL ine,$arrMatches );

    Maybe theres a simpler way, I'm tired |-)

    Comment

    • Alan Little

      #3
      Re: How do you split a string using split()?

      Carved in mystic runes upon the very living rock, the last words of ImOk
      of comp.lang.php make plain:
      [color=blue]
      > I have this PHP statement which works fine and breaks down a string of
      > lines delimited with a \r into an array.
      >
      > $arrList=split( "\r", $strList);
      >
      > But what I really want is to be able to tell it to split out only the
      > strings that start with the letter "b".
      >
      > E.g. I thought this would work "b\W\r"
      >
      > I tried every regular expression but cant figure it out. I could use
      > some other function combinations to achive this but I though if there
      > is a way to do it with just spli()[/color]

      Since split() deletes the delimiter string that it's splitting on, I
      don't think that function is going to give you what you want, regardless
      of regex.

      Try the preg_match_all( ) function.

      An alternative would be to convert the appropriate \r characters to
      something else unlikely to be in the string (such as a \3, for example),
      and split on that:

      $strList = preg_replace("/(b.+)\r/U", "\\1\3", $strList);
      $arrList = explode("\3", $strList);

      --
      Alan Little
      Phorm PHP Form Processor

      Comment

      • ImOk

        #4
        Re: How do you split a string using split()?

        Based on your suggestions, I finally figured out a solution.

        $strList="aabbc c\raaxxdd\rbbaa cc\rbcxxx\rcaax x\rcbxx\rabcdr\ rbcd";
        $arrList=split( "\r", $strList);
        $arrFiltered=pr eg_grep("/^b(.+)/",$arrList) ;

        Split string into the array components and using preg_grep filter only
        the elements that start with 'b'

        Thanks

        Comment

        Working...