搜索字符串并在PHP中创建数组

时间:2017-10-11 12:09:08

标签: php string search

如何转换此字符串

 *|text:student:required|* 

(*和|是字符串的一部分)就像这样

['text' ,'student','required']  

4 个答案:

答案 0 :(得分:2)

你走了:

$str = "|text:student:required|";
$str = trim($str,"|");
$str = trim($str,"*");
$x = explode(':',$str);
print_r($x);die;

答案 1 :(得分:2)

尝试以下方法:

$str = '*|text:student:required|*';
$str = preg_replace("/[|*]/", '', $str);
$arr = explode(':', $str);

这只是使用|从字符串中删除* AND preg_replace(),并使用explode将字符串转换为数组

答案 2 :(得分:1)

preg_split函数中最短的函数:

$s = '*|text:student:required|* ';
$result = preg_split('/[*:| ]+/', $s, -1, PREG_SPLIT_NO_EMPTY);

print_r($result);

输出:

Array
(
    [0] => text
    [1] => student
    [2] => required
)

答案 3 :(得分:0)

$string = "*|text:student:required|";
$string = str_replace("*", "", str_replace("|","", $string));
$array = explode(':', $string);