Reputation: 776
I've got this sentence from database
0| Future plan 1| Low 2| Normal 3| High 4| Highest
And I've need to make it look similar to:
[0] => Future plan
[1] => Low
[2] => Normal
[3] => High
[4] => Highest
I cant find any thread, similar to this. Help please.
Upvotes: 1
Views: 115
Reputation: 1577
@ankhzet pointed out a flaw which this answer covers.
<?php
$v = '0| Future plan 22 2| Normal 123| Low 3| High 4| Highest';
$arraySplit = preg_split("/([0-9]+)\|/", $v, -1, PREG_SPLIT_DELIM_CAPTURE);
$arrayResult = array();
for($i = 1; $i < count($arraySplit); $i+=2){
$arrayResult[$arraySplit[$i]] = $arraySplit[$i+1];
}
pre($arrayResult);
?>
New output:
Array
(
[0] => Future plan 22
[2] => Normal
[123] => Low
[3] => High
[4] => Highest
)
OLD
This method will maintain the ID's and is shorter than @ankhzet's answer. Here if you need it:
preg_match_all("/(\d+)\|([^0-9]+)/", $v, $array);
$array = array_combine($array[1], $array[2]);
pre($array);
Output looks like this:
Array
(
[0] => Future plan
[2] => Normal
[1] => Low
[3] => High
[4] => Highest
)
FYI, pre()
is just a function the wraps print_r()
inside <pre></pre>
tags.
Upvotes: 2
Reputation: 2568
REGEXP, suggested by @Epodax won't keep indexes:
$v = '0| Future plan 2| Normal 1| Low 3| High 4| Highest';
$array = preg_split("/[0-9]+\|/", $v, -1, PREG_SPLIT_NO_EMPTY);
dump($array);
// array:5 [▼
// 0 => " Future plan "
// 1 => " Normal "
// 2 => " Low "
// 3 => " High "
// 4 => " Highest"
// ]
If that matters, probably better be to use this:
$v = '0| Future plan 2| Normal 1| Low 3| High 4| Highest';
$v .= '0|';
$r = [];
while ($v) {
if (!preg_match('/(\d+)\|(.+?)(?:(\d+)\|)/', $v, $m, PREG_OFFSET_CAPTURE))
break;
$idx = intval($m[1][0]);
$r[$idx] = trim($m[2][0]);
if (($offset = $m[3][1]) >= strlen($v))
break;
$v = substr($v, $offset);
}
dump($r);
// array:5 [▼
// 0 => "Future plan"
// 2 => "Normal"
// 1 => "Low"
// 3 => "High"
// 4 => "Highest"
// ]
Upvotes: 1
Reputation: 1828
I found something that SHOULD do the trick for you: preg_split
$string = "0| Future plan 1| Low 2| Normal 3| High 4| Highest";
$array = preg_split("/[0-9]+\|/", $string, -1, PREG_SPLIT_NO_EMPTY);
Upvotes: 5
Reputation: 2500
Use explode
$words = preg_replace('/[0-9]+/', '', $string);//remove all numbers from string
$array = explode ("|" , $words);
$array2 = array_filter($array)//remove empty elements
Upvotes: 1
Reputation: 9432
Given that in your string you have |
, I made the following (extra checks must be made):
$string = '0| Future plan 1| Low 2| Normal 3| High 4| Highest';
$string = str_replace('|','',$string); //replace |
$length = strlen($string); //get length
for($i=0; $i<=$length-1;$i++)
{
if(is_numeric($string[$i])) { //get numbers
$word[$string[$i]] ='';
$lastIndex = $string[$i]; //save last found
} else {
if($string[$i]!=' ') { //if not space append to array element of last number found
$word[$lastIndex] .= $string[$i];
}
}
}
var_dump($word);
array (size=5)
0 => string 'Futureplan' (length=10)
1 => string 'Low' (length=3)
2 => string 'Normal' (length=6)
3 => string 'High' (length=4)
4 => string 'Highest' (length=7)
Upvotes: 1
Reputation: 11
First change database sentence and then in your case use php function explode ("| " , $string)
. This function can explode the string to array by delimiter.
Upvotes: 1
Reputation: 9358
try this:
<?php
$str = "Future plan | Low | Normal | High | Highest";
print_r (explode("|",$str));
?>
Upvotes: 1