我有这样的字符串:
$string1 = '35 Hose & Couplings/350902 GARDEN HOSE COUPLING, PVC, 16\/19 MM"';
$string2 = '35 Hose & Couplings/350904 GARDEN HOSE TAP CONNECTOR, PVC, 3\/4" FEMALE THREAD"';
我尝试将字符串分开并将其转换为如下数组:
$name1 = explode('/', $string1);
$name1 = trim(end($name1));
$name2 = explode('/', $string2);
$name2 = trim(end($name2));
/*
#results
$name1[0] = '35 Hose & Couplings';
$name1[1] = '350902 GARDEN HOSE COUPLING, PVC, 16\';
$name1[2] = '19 MM"';
...
#expected results
$name1[0] = '35 Hose & Couplings';
$name1[1] = '350902 GARDEN HOSE COUPLING, PVC, 16\/19 MM"';
...
*/
我想在仅/
时展开字符串,但当它遇到\/
时, 不应该 爆炸字符串,如果字符串包含\/
,我的代码仍然会爆炸,有没有办法做到这一点?
答案 0 :(得分:3)
答案 1 :(得分:1)
你可以这样:
$string1 = str_replace("\/","@",$string1);
$name1 = explode('/', $string1);
foreach($name1 as $id => $name) {
$name1[$id] = str_replace("@","\/",$name1[$id]);
}
我知道,有点麻烦,但应该做的伎俩。将其包装在一个函数中以提高可读性。
基本上我用一个临时字符串替换了你不想爆炸的字符串,并在爆炸后将其恢复。