Php: Split String into Array, Like Explode with No Delimiter

PHP: Split string into array, like explode with no delimiter

$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");

str_split takes an optional 2nd param, the chunk length (default 1), so you can do things like:

$array = str_split("aabbccdd", 2);

// $array[0] = aa
// $array[1] = bb
// $array[2] = cc etc ...

You can also get at parts of your string by treating it as an array:

$string = "hello";
echo $string[1];

// outputs "e"

Empty delimiter Warning when using PHP explode() function

If dealing with multi-byte UTF-8 strings you should use:

$array = preg_split('//u', $My_String,-1, PREG_SPLIT_NO_EMPTY);

Otherwise you can just use:

$array = str_split($My_String);

The reason is noted in the manual:

str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string.

Starting from PHP version 7.4 the mbstring equivalent of str_split was added so you can now use:

$array = mb_str_split($my_string);

mb_str_split manual page

Explode does not split my string because of a special character

Depending on your encoding, try something like:

explode(chr(149), $text);  //for ISO-8859-1

Or

explode(utf8_encode('•'), $text);  //for UTF-8

php explode all characters

As indicated by your error, explode requires a delimiter to split the string. Use str_split instead:

$arr = str_split('testing');

Output

Array
(
[0] => t
[1] => e
[2] => s
[3] => t
[4] => i
[5] => n
[6] => g
)

Convert sentence to array of words without explode or split method in php

The main issue is that when you call substr() the third parameter is the length of the string you want and not the position, so just subtract the $currentindex from it...

$temp = substr($a, $currentindex, $i - $currentindex);

You are also missing the last part, so after the loop add (you can just take the rest of the string in this case)...

$output[] = substr($a, $currentindex);

Explode string into array with no empty elements?

Try preg_split.

$exploded = preg_split('@/@', '1/2//3/', -1, PREG_SPLIT_NO_EMPTY);

I am trying to split/explode/preg_split a string but I want to keep the delimiter

You can use preg_match_all like so:

$matches = array();
preg_match_all('/(\/block\/[0-9]+\/page\/[0-9]+)/', '/block/2/page/2/block/3/page/4', $matches);
var_dump( $matches[0]);

Output:

array(2) {
[0]=>
string(15) "/block/2/page/2"
[1]=>
string(15) "/block/3/page/4"
}

Demo

Edit: This is the best I could do with preg_split.

$array = preg_split('#(/block/)#', '/block/2/page/2/block/3/page/4', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

$result = array();
for( $i = 0, $count = count( $array); $i < $count; $i += 2)
{
$result[] = $array[$i] . $array[$i + 1];
}

It's not worth the overhead to use a regular expression if you still need to loop to prepend the delimiter. Just use explode and prepend the delimiter yourself:

$delimiter = '/block/'; $results = array();
foreach( explode( $delimiter, '/block/2/page/2/block/3/page/4') as $entry)
{
if( !empty( $entry))
{
$results[] = $delimiter . $entry;
}
}

Demo

Final Edit: Solved! Here is the solution using one regex, preg_split, and PREG_SPLIT_DELIM_CAPTURE

$regex = '#(/block/(?:\w+/?)+(?=/block/))#';
$flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
preg_split( $regex, '/block/2/page/2/block/3/page/4', -1, $flags);
preg_split( $regex, '/block/2/page/2/order/title/sort/asc/block/3/page/4', -1, $flags);

Output:

array(2) {
[0]=>
string(15) "/block/2/page/2"
[1]=>
string(15) "/block/3/page/4"
}
array(2) {
[0]=>
string(36) "/block/2/page/2/order/title/sort/asc"
[1]=>
string(15) "/block/3/page/4"
}

Final Demo

Splitting strings in PHP and get the last part

  • preg_split($pattern,$string) split strings within a given regex pattern
  • explode($pattern,$string) split strings within a given pattern
  • end($arr) get last array element

So:

$strArray = explode('-',$str)
$lastElement = end(explode('-', $strArray));
// or
$lastElement = end(preg_split('/-/', $str));

Will return the last element of a - separated string.


And there's a hardcore way to do this:

$str = '1-2-3-4-5';
echo substr($str, strrpos($str, '-') + 1);
// | '--- get the last position of '-' and add 1(if don't substr will get '-' too)
// '----- get the last piece of string after the last occurrence of '-'

PHP split string into two arrays - values split and delimiters

Use PHP Regular Expression. The preg_match_all() function in your case:

Live Demo

Code:

$input = "Name=John AND State=GA OR State=CA";

preg_match_all("/[a-zA-Z]+\s*=\s*[a-zA-Z]+/", $input, $output_1);
$output_1 = $output_1[0];

preg_match_all("/AND|OR/", $input, $output_2);
$output_2 = $output_2[0];

print_r($output_1);
print_r($output_2);

Output:

Array
(
[0] => Name=John
[1] => State=GA
[2] => State=CA
)
Array
(
[0] => AND
[1] => OR
)


Related Topics



Leave a reply



Submit