如何爆炸字符串并对所选属性进行回声

时间:2011-07-07 16:36:38

标签: php

我有一个包含的字符串;细分,并希望对所选值进行回声。

我的字符串是$string='Width,10;Height,5;Size,1,2,3'

我想对高度值进行回显(回声结果必须为5)

3 个答案:

答案 0 :(得分:2)

$parts = explode(';', $string);
$component = explode(',', $parts[1]); // [1] is the Height,5 portion
echo $component[1]; // 5

答案 1 :(得分:1)

或者这个:

$p = explode(';', $string);
$data = array();
foreach($p as $part) {
    $split = explode(',',$part,2); //the 'Size' bit different that the rest. I assume 1,2,3 is the value for Size?
    $data[$split[0]] = $split[1];
}
$what_you_want_to_find = 'Height';
echo $data[$what_you_want_to_find]; 

答案 2 :(得分:0)

试试这个:

$attrs = explode(";", $string);
$attrHeight = "";
foreach ($attrs as $value) {

   if (strpos($value, "Height") !== false) 
     $attrHeight = explode(",", $value);

}

echo $attrHeight;