Re: Re: Separating spaces from the rest

From: Date: Sat, 01 May 2004 12:12:25 +0000
Subject: Re: Re: Separating spaces from the rest
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-185094@lists.php.net to get a copy of this message
Curt Zirzow wrote:
You forgot the pattern deliminator: print_r(preg_split('/(\s+)/', " word1 word2 word3.", -1, PREG_SPLIT_DELIM_CAPTURE)); Curt
You're right, that fixed the space capturing. Justin Patrin wrote:
...I wonder why you're doing this, but here's an answer for you. BTW, if you use var_dump or var_export, you can see the whitespace :-). $arr = preg_split('/(^\s+)/', " word1 word2 word3.", -1,
      PREG_SPLIT_DELIM_CAPTURE);
The ^ means beginning of string. This will still give you an empty string in $arr[0], so do something like:
unset($arr[0]); $arr = array_values($arr); Or just write your code to expect the extra entry. ;-)
The ^ did the trick, thanks! I noticed the empty [0]. I'm doing this to display code in an indented way and to keep the indent if the line wraps, instead of going back all the way to the left. So I'm putting the spaces in a <td> and the code line in another <td>, with a table for each line. I tested it and looks very well, now I'm writting the code to generate it. Thank you all for your help! :) Anguz

« previous php.general (#185094) next »