Jump to content

Preg_split and delimiter


Mastodont

Recommended Posts

Probably lamer question, but I really don't understand ... wanna break content of textarea into lines.

 

This works OK, each item of array contains one line:

 

$s = preg_split("\r?\n?", $_POST['...']);

 

Array ([0] => first [1] => second [2] => third )

 

With delimiters, array is composed from individual characters :-(((

 

$s = preg_split("/\r?\n?/", $_POST['...']);

 

Array ([0] => [1] => f [2] => i [3] => r [4] => s [5] => t [6] => [7] => s [8] => e [9] => c [10] => o [11] => n [12] => d [13] => [14] => t [15] => h [16] => i [17] => r [18] => d [19] => )

 

WTF? Thanks for explanation.

Link to comment
https://forums.phpfreaks.com/topic/85223-preg_split-and-delimiter/
Share on other sites

~a?~

 

on:

hi
hi

 

yields:

Array
(
    [0] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
        )

)

 

There are 6 characters including linebreaks in the haystack on an optional ? it will try to match it for each character in the haystack, and if not it will produce a null group/capture

This is what it looks like..

 

So \r? which is not found in your haystack (which you did not provide so i'm guessing based on my testing) It gives captures all null for each char it checks against

 

Also I noticed '\r?\n?' produces a parse php error that says it needs delimiters, while "\r?\n?" doesn't

 

 

Here are some interesting notes on the function:

http://us.php.net/manual/en/function.preg-split.php#59358

http://us.php.net/manual/en/function.preg-split.php#51209

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.