php字符串分割问题

来源:百度知道 编辑:UC知道 时间:2024/07/13 01:49:36
1@A|||5@B|||6@C|||4@D|||2@E|||3@F|||7@G|||8@H||| 这是源字符串:

想做成这样的数组:1=>A,5=>B,6=>C 就这个意思!

<?php
$str = '1@A|||5@B|||6@C|||4@D|||2@E|||3@F|||7@G|||8@H|||';

##---------正则表达式方法
//这是你要的结果数组
$result = array();
preg_match_all('/(\d+)@(\w+)/', $str, $matchs);
#print_r($matchs);//你能明白$matchs是什么东西
foreach ($matchs[0] as $i=>$tmp){
//$matchs[1][$i] 就是1 $matchs[2][$i] 就是A
$result[$matchs[1][$i]] = $matchs[2][$i];
}

print_r($result);

unset($result);
#--------字符串方法
#以|||分割成1@A
$ps = split("\|\|\|", $str);
#var_dump($matchs);
foreach ($ps as $s){
//以@分割1@A=> 1, A
if(!$s){
continue;
}
list($num, $word) = split("@", $s);
$result[$num] = $word;
}
print_r($result);

|||为分割符,
explode — 使用一个字符串分割另一个字符串
说明
array explode ( string $separator , string $string [, int $limit ] )
此函数返回由字符串组成的数组,每个元素都是 string 的一个子串,它们被字符串 separator 作为边界点分割出来。如果设置了 limit 参数,则返回的