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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.