How to split a string by string in PHP?

How to split a string by string in PHP? For example,

"a string   separated by space" =>
["a", "string", "separated", "by", "space"]

and

"a,string,separated,by,comma" =>
["a", "string", "separated", "by", "comma"]

The commonly used `explode()` is not enough here because multiple delimiters should be consider as one (such as " ", 3 spaces together, is consider a single delimiter). Instead, the `preg_split()` function can be used to use regular expression to handle such cases.

One example is as follows.

$ php -a
Interactive mode enabled

php > $str = "a string   separated by space";
php > $splits = preg_split('/\s+/', $str);
php > print_r($splits);
Array
(
    [0] => a
    [1] => string
    [2] => separated
    [3] => by
    [4] => space
)
php > 

For another example,

php > $str = "a,string,separated,by,comma";
php > $splits = preg_split('/,+/', $str);
php > print_r($splits);
Array
(
    [0] => a
    [1] => string
    [2] => separated
    [3] => by
    [4] => comma
)

Similar Posts

  • | |

    Release Notes For Linux v2.0

    This is the release notes for linux release v2.0 (source code: linux-2.0.tar.gz) with format adjusted by removing/replacing tabs/spaces/new lines/formatting marks. This notes document can give us an understanding of the early development of the Linux kernel. The original ASCII formatted version is at the end of this post. Intro This document contains a list of…

  • Good Cinnamon theme for Fedora 21 Linux?

    Any suggestion on good theme configuration for Fedora 21 Linux? My favorite combination of Window borders, Icons, Controls, Mouse Pointer and Desktop is as follows. All packages and themes are from Fedora repository or the Cinnamon repository. Remember to set the cursor theme for QT application like Google Chrome following: https://www.systutorials.com/4068/configuring-mouse-cursor-style-for-qt-applications-in-gnome-mate-desktop/ For this configuration, the…

  • How to change the fonts of gnome-shell for gnome3?

    How to change the fonts of gnome-shell for gnome3? Like the system bar. The fonts of gnome-shell is described in its own theme. For the default gnome-shell, you need to edit the file as root: /usr/share/gnome-shell/theme/gnome-shell.css Right, it is a css file. Find the font-family keyword and change the corresponding fonts. Then you can restart…

  • Xen HVM DomU configuration file

    An example of Xen HVM DomU configuration file. An example for install the OS from an ISO: name=”10.0.1.235″ vcpus=2 memory=2048 shadow_memory=8 disk=[‘file:/lhome/xen/vm-10.0.1.235/vmdisk0,xvda,w’, ‘file:/lhome/Linux-x86_64-DVD.iso,xvdc:cdrom,r’] vif=[‘bridge=xenbr0′] kernel=’/usr/lib/xen/boot/hvmloader’ builder=’hvm’ device_model=’/usr/lib64/xen/bin/qemu-dm’ extra=” vnc=1 vnclisten=”0.0.0.0″ vncpasswd=’1234567′ # vncdisplay=1 vncconsole=1 on_reboot=’restart’ on_crash=’restart’ An example for run the VM after installation: name=”10.0.1.235″ vcpus=2 memory=2048 shadow_memory=8 disk=[‘file:/lhome/xen/vm-10.0.1.235/vmdisk0,xvda,w’] vif=[‘bridge=xenbr0′] kernel=’/usr/lib/xen/boot/hvmloader’ builder=’hvm’ device_model=’/usr/lib64/xen/bin/qemu-dm’ extra=” vnc=1…