PHP split number from string

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

The optional input parameter limit is used to signify the number of elements into which the string should be divided, starting from the left end of the string and working rightward.

The PHP explode() function converts a string to an array. Each of the characters in the string is given an index that starts from 0. Like the built-in imlode() function, the explode function does not modify the data (string).

Syntax of the explode() Function

The explode() function takes in three parameters:

  • the separator
  • the string to convert to an array
  • and the limit

The full syntax looks like this:

explode(separator, string, limit)

Unlike implode() which works even if the separator is not provided, the explode() function won’t work without the separator. So, just like the string split into an array, the separator is required. You can use the limit parameter to specify the number of arrays expected. It is optional.

Examples of implode()

Let's say that I have the string "Hello World". If the string is passed into an explode() function,

$str = "Hello world";
$newStr = explode(" ", $str);

// We are printing an array, so we can use print_r()
print_r($newStr); 
3 takes an index of 0 in the array, and
$str = "Hello world";
$newStr = explode(" ", $str);

// We are printing an array, so we can use print_r()
print_r($newStr); 
4 takes an index of 1. Remember that arrays use zero-based indexing.

$str = "Hello world";
$newStr = explode(" ", $str);

// We are printing an array, so we can use print_r()
print_r($newStr); 

PHP split number from string

If you specify a limit in the explode() function, the index(es) won’t be more than that number. For example, if you specify 2, all the strings would show, but the index won’t be more than 2.

$str = "CSS, HTML, PHP, Java, JavaScript";
$newStr = explode(" ", $str, 2);

// We are printing an array, so we can use print_r()
print_r($newStr); 

PHP split number from string

You can see that the first element takes an index of 0 and the rest of the comma-separated elements take 1. The index is not more than the limit of 2 specified.

The explode() function looks at spaces in the string to split the string into an array. If you type two different words together, they are treated as one:

$str = "CSS HTMLPHP Java JavaScript";
$newStr = explode( " ", $str);

// We are printing an array, so we can use print_r()
print_r($newStr); 

PHP split number from string

You can see that HTML and PHP got ptinted together because there was no space between them.

Conclusion

This article showed you how to use the explode() function in PHP.

Note that unlike implode() which works without the separator, the separator is very important in explode(). If you don’t specify a separator, explode() won’t work as expected.

$str = "CSS, HTML, PHP, Java, JavaScript";
$newStr = explode($str, 2);

// We are printing an array, so we can use print_r()
print_r($newStr); 

PHP split number from string

And if you leave the separator as an empty string, you get an error:

PHP split number from string

Thank you for reading.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


PHP split number from string
Kolade Chris

Web developer and technical writer focusing on frontend technologies.


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

❮ PHP String Reference

Example

Split the string "Hello" into an array:

print_r(str_split("Hello"));
?>

Try it Yourself »


Definition and Usage

The str_split() function splits a string into an array.


Syntax

Parameter Values

ParameterDescriptionstringRequired. Specifies the string to splitlengthOptional. Specifies the length of each array element. Default is 1

Technical Details

Return Value:If length is less than 1, the str_split() function will return FALSE.  If length is larger than the length of string, the entire string will be returned as the only element of the array.PHP Version:5+

More Examples

Example

Using the length parameter:

print_r(str_split("Hello",3));
?>

Try it Yourself »


❮ PHP String Reference

How to separate a number from a string in PHP?

Method 1: Using preg_match_all() Function. Note: preg_match() function is used to extract numbers from a string..
Using preg_match_all() Function..
Using filter_var() Function..
Using preg_replace() function..

How do I split a number in a string?

Determine the string's length..
Individually scan each character (ch) in a string. Add it to the res1 string if (ch is a digit). ... .
Print every string. We shall have three strings: one with a numeric component, one without a numeric component, and one with special characters...

How to split a value in PHP?

PHP | explode() Function explode() is a built in function in PHP used to split a string in different strings. The explode() function splits a string based on a string delimiter, i.e. it splits the string wherever the delimiter character occurs.

How to extract value from string in PHP?

Answer: Use the PHP substr() function The PHP substr() function can be used to get the substring i.e. the part of a string from a string. This function takes the start and length parameters to return the portion of string.