InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

PHP

    PHP strings

    • PHP - split string examples
    • PHP - take last n characters of a string
    • PHP check if string contains something

    PHP Array

    • PHP - associative array value in double quoted string
    • PHP - check if a value is in array
    • PHP - convert array to associative array
    • PHP - print array in one line
    • PHP array foreach - code snippets
    • PHP array map example
    • PHP check if key exists in array
    • PHP sort associative array using custom compare function
    • php get array value with default

    PHP Regex

    • PHP Regex

    PHP Cookbook

    • PHP - assign multi lines string to a variable
    • PHP - call a function with arguments in array examples
    • PHP - get calling file name and line number using debug_backtrace
    • PHP - get class name and file name from an object
    • PHP - get function arguments examples
    • PHP - how to catch errors using set_error_handler
    • PHP - how to initialize static variables
    • PHP - how to use function static variable as cache
    • PHP check if a filename is valid
    • PHP elapsed time
    • PHP execute command and capture shell exit status
    • PHP how to get current url
    • PHP print number to two decimal places
    • PHP remove trailing whitespaces and newline
    • Php equal (==) vs identical (===)
    • Running php eval on code with tags

    PHP functions online

    • PHP functions online
     
    • Home
    • > Tutorials
    • > PHP

    PHP – split string examples

    By admin on Jan 26, 2016

    Splitting a string/text at a delimiter is very common use case in php. One can either use php explode (split on a fixed delimiter) or preg_split (split on regex delimiter). Using explode is generally good enough for most use cases. Also note that php function split is now deprecated from PHP 5.3. So we’ll not cover that in this tutorial. Here are some examples using explode and preg_split.

    PHP explode on space with no limit

    This example splits a string on single space. In case there are two spaces, one empty element will also be present in returned array.

    <?php
    $s="hello world. hello  word.\n2ndline";
    $arr = explode(" ", $s);
    print_r($arr);
    ?>
    Array
    (
        [0] => hello
        [1] => world.
        [2] => hello
        [3] => 
        [4] => word.
    2ndline
    )
    
    Env: PHP 7.3.18 (Linux)

    PHP explode on space with limit 2

    Here split is done on first space and rest of the string gets captured in 2nd element. In case there is no space, only one element array is returned.

    <?php
    $s="hello world. hello  word.\n2ndline";
    $arr = explode(" ", $s, 2);
    print_r($arr);
    ?>
    Array
    (
        [0] => hello
        [1] => world. hello  word.
    2ndline
    )
    
    Env: PHP 7.3.18 (Linux)

    PHP preg_split on any whitespace sequence

    Using preg_split gives us flexibility to split on a regex. Here is an example where we split on any size whitespace string.

    <?php
    $s="hello world. hello  word.\n2ndline";
    $arr = preg_split('/\s+/', $s);
    print_r($arr);
    ?>
    Array
    (
        [0] => hello
        [1] => world.
        [2] => hello
        [3] => word.
        [4] => 2ndline
    )
    
    Env: PHP 7.3.18 (Linux)

    Note that we had two whitespace here. preg_split regex took care of that. Newline is also treated as whitespace.

    Suggested posts:

    1. Python string split examples
    2. PHP remove non printable characters from a string
    3. Php regex delimiter examples
    4. PHP beginning and end of string regex examples
    5. PHP check if string contains something
    6. PHP regex – whitespace shorthand (\s) regex examples
    7. PHP – associative array value in double quoted string
    8. PHP – take last n characters of a string
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged PHP, PHP cookbook, PHP strings, PHP text processing, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2023 InfoHeap.

    Powered by WordPress