PHP

Topic: String

How To Split a String into Pieces?

There are two functions you can use to split a string into pieces: explode(substring, string) - Splitting a string based on a substring. Faster than split(). split(pattern, string) - Splitting a string based on a regular expression pattern. Better than explode() in handling complex cases.Both functions will use the given criteria, substring or pattern, to find the splitting points in the string, break the string into pieces at the splitting points, and return the pieces in an array. Here is a PHP script on how to use explode() and split():<?php $list = explode("_","php_strting_function.html");print("explode() returns:\n");print_r($list);$list = split("[_.]","php_strting_function.html");print("split() returns:\n");print_r($list);?>This script will print:explode() returns:Array(    [0] => php    [1] => strting    [2] => function.html)split() returns:Array(    [0] => php    [1] => strting    [2] => function    [3] => html)The output shows you the power of power of split() with a regular expression pattern as the splitting criteria. Pattern "[_.]" tells split() to split whenever there is a "_" or ".".

Browse random answers:

How To Count the Number of Characters in a String?
How Many ways to Remove Leading and Trailing White Spaces?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
What Is the Best Way to Test the strpos() Return Value?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?
How To Convert Strings to Upper or Lower Cases?
How To Convert Leading Characters to Upper Case?
How To Compare Two Strings with strcmp()?
How To Convert Strings to Hex Numbers?
How To Generate a Character from an ASCII Value?
How To Convert a Character to an ASCII Value?
How To Split a String into Pieces?
How To Join Multiple Strings into a Single String?
How To Replace a Group of Characters by Another Group?
How Many Escape Sequences Are Recognized in Single-Quoted Strings?
Can You Specify the "new line" Character in Single-Quoted Strings?
What Are the Special Characters You Need to Escape in Double-Quoted Stings?
How To Include Variables in Double-Quoted Strings?
How Many Ways to Include Array Elements in Double-Quoted Strings?
How To Access a Specific Character in a String?
How To Assigning a New Character in a String?
How To Concatenate Two Strings Together?
How To Compare Two Strings with Comparison Operators?
How To Convert Strings to Numbers?
How To Count the Number of Characters in a String?
How To Remove the New Line Character from the End of a Text Line?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
How To Remove the New Line Character from the End of a Text Line?
How to Find a Substring from a Given String?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?