php split alternative

Posted on 2011-10-02
Medium Priority
Last Modified: 2012-05-12
how can I do the latest version of php

$sData = split("[\n]", $message);

Open in new window

Question by:XK8ER
  • 2
LVL 18

Expert Comment

by:Matthew Kelly
ID: 36900694
Explode is the alternative: http://php.net/manual/en/function.explode.php
LVL 18

Accepted Solution

Matthew Kelly earned 2000 total points
ID: 36900699
For more information: see the Notes section of the PHP split page: http://php.net/manual/en/function.split.php

"preg_split(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to split(). If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine."

Splitting on new line characters wouldn't require regular expressions so exploded() would be faster in your case.

LVL 111

Expert Comment

by:Ray Paseur
ID: 36900948
The \n thing that you refer to is a newline character.  There is a context-aware built-in constant called PHP_EOL that should be used in explode() to break a file up into individual lines in an array.  The PHP_EOL character is different in different installations of PHP, and that is why you want to use the constant instead of some kind of combination of \n \r \r\n, etc.

Example in the code snippet.
$sData = explode(PHP_EOL, $message);

Open in new window


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question