php split alternative

Posted on 2011-10-02
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
    LVL 18

    Expert Comment

    by:Matthew Kelly
    Explode is the alternative:
    LVL 18

    Accepted Solution

    For more information: see the Notes section of the PHP split page:

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

    Expert Comment

    by:Ray Paseur
    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    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 count occurrences of each item in an array.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now