Coming from PERL I need help with a code translation to PHP. I'm trying to parse all the words in submitted text into an array. Each word is demarcated by white space (\s in PERL) before and behind. The code to do this in PERL is below. What's the equivalent in PHP?

<code>
$_ = $someText;
@a = /\s(.*?)\s/gsm;
</code>

thanks,

chris

    preg_match_all('/\s(.[\n])\s/',$text,$array);
    //$array is an array containing all character data and minus spaces and newlines.
    print_r($array);

    ...note: I removed your modifiers because /g is defualt in PHP, /s is confusing becuase we trying to match by \s anyway
    and /m is for multiline, which wont work if we don't use $ respctively to begin and end our regex.

      If there is always one space between words you can use explode.

      If there can be more spaces or also tabs and newlines between words you can use preg_split:

      $words = preg_split('/\s+/', $str);

      The preg-functions have (almost) the same syntax as perl regular expressions.

        Ya, but that would still contain newlines.

          <?
          $str = "Word1 Word2 Word3\t\tWord4\n\nWord5\n\r\t \tWord6";
          $words = preg_split('/\s+/', $str);
          print_r($words);
          ?>

          Output:

          Array
          (
          [0] => Word1
          [1] => Word2
          [2] => Word3
          [3] => Word4
          [4] => Word5
          [5] => Word6

          )

            Write a Reply...