w3resource

PHP: explode() function

Description

The explode() function is used to split a string.

Version:

(PHP 4 and above)

Syntax:

explode(delimiter, string_name, limit) 

Parameters:

Name Description Required /
Optional
Type
delimiter Sets the boundary string within the input string. Required String
string_name The string to be split. Required String
limit Refers to the maximum number of elements in the output array if set to +ve value. If set to -ve value, all but the last element will be present in the output array. Optional Integer

Return values:

An array of strings.

Value Type: Array.

Pictorial Presentation

php-string-explode()

Example:

<?php
$class_list='V,VI,VII,VIII,IX,X';
$classes=explode(",",$class_list);
print_r($classes);
?>

Output :

 Array  (      [0] => V      [1] => VI      [2] => VII      [3] => VIII      [4] => IX      [5] => X  )

View the example in the browser

See also

PHP Function Reference

Previous: crypt
Next: fprintf



PHP: Tips of the Day

PHP: Using str_replace so that it only acts on the first match?

Can be done with preg_replace:

function str_replace_first($from, $to, $content)
{
    $from = '/'.preg_quote($from, '/').'/';

    return preg_replace($from, $to, $content, 1);
}

echo str_replace_first('abc', '123', 'abcdef abcdef abcdef'); 
// outputs '123def abcdef abcdef'

The magic is in the optional fourth parameter [Limit]. From the documentation:

[Limit] - The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

Ref : https://bit.ly/35SCuP8