使用explode()或preg_split()将数组元素中的字符串拆分为数组

时间:2012-04-12 21:31:33

标签: php string preg-split

我有一个数组,每个元素都是这样的字符串:

$items[0] = "*1*x *2858* in *1* with selected image to print at *http://mysite.com/test.jpg*"

$items[1] = "*2*x *2353* in *2* with selected image to print at *http://mysite.com/test1.jpg*"

如何将此数组元素拆分为第二维数组,使其变为:

$items[0][quantity] == 1
$items[0][product_id] == 2858
$items[0][variation} == 1
$items[0][selectedImageUrl] == "http://mysite.com/test.jpg"

$items[1][quantity] == 2
$items[1][product_id] == 2353
$items[1][variation} == 2
$items[1][selectedImageUrl] == "http://mysite.com/test1.jpg"

非常感谢你的帮助!


@Cal

这就是我在申请代码时所拥有的。

我将它应用到我的情况中,这就是我所拥有的:

function parse_my_str($str){
  $bits = explode(' ', $str);
  $out['selectedImageUrl'] = array_pop($bits);
  $out['product_id'] = $bits[1];
  $out['variation'] = $bits[3];
  $bits = explode('*', $str);
  $out['quantity'] = $bits[1];
  return $out;
}

$items = explode("||", $_SESSION['mp_shipping_info']['special_instructions']); 
foreach ($items as $i => $v) {
    $items[$i] = parse_my_str($v);
    print_r($items[$i]);
}

但我已经

Array ( [selectedImageUrl] => *http://youparkrudely.com/files/2012/04/2011-Aston-Martin-Rapide-026.jpg* [product_id] => *2858* [variation] => *1* [quantity] => 1 ) Array ( [selectedImageUrl] => [product_id] => [variation] => [quantity] => )

@Cal

    Array ( 
[selectedImageUrl] => *http://youparkrudely.com/files/2012/04/2011-Aston-Martin-Rapide-026.jpg* 
[product_id] => *2858* 
[variation] => *1* 
[quantity] => 1 ) 
Array ( [selectedImageUrl] => [product_id] => [variation] => [quantity] => )

3 个答案:

答案 0 :(得分:1)

针对新字符串进行了更新,改为使用preg_match()

$str = "*1*x *2858* in *1* with selected image to print at *http://mysite.com/test.jpg*";
$arr = parse_my_str($str);
print_r($arr);

function parse_my_str($str){
  preg_match_all('!\\*(.*?)\\*!', $str, $m);
  return array(
    'quantity' => $m[1][0],
    'product_id' => $m[1][1],
    'variation' => $m[1][2],
    'selectedImageUrl' => $m[1][3],
  );
}

对于您的示例,您将使用如下函数:

foreach ($items as $k => $v) $items[$k] = parse_my_str($v);

答案 1 :(得分:1)

您可以使用preg_match()解决方案和正则表达式。不确定我是否完全匹配,但这是一个简单的例子。

// Regular Expression
$pattern = '/^\*(\d+)\*x (\d+) in (\d+) with selected image to print at (.+)$/';
preg_match( $pattern, $string, $matches );

如果您var_dump() $matches,您将获得此信息:

array(5) {
[0]=> string(74) "*2*x 2353 in 2 with selected image to print at http://mysite.com/test1.jpg" 
[1]=> string(1) "2" 
[2]=> string(4) "2353" 
[3]=> string(1) "2" 
[4]=> string(27) "http://mysite.com/test1.jpg" 
}

你必须遍历你的项目数组。

foreach ( $array as $key => $value ) :

    preg_match( $pattern, $value, $matches );

    $items[$key]['quantity'] = $matches[1];
    $items[$key]['product_id'] = $matches[2];
    $items[$key]['variation'] = $matches[3];
    $items[$key]['selectedImageURL'] = $matches[4];

endforeach;

我还建议使用自定义功能。

如果您需要有关正则表达式的帮助,我建议从这里开始:http://www.regular-expressions.info/

答案 2 :(得分:0)

<?php
$items[0] = "*1*x 2858 in 1 with selected image to print at http://mysite.com/test.jpg";
$items[1] = "*2*x 2353 in 2 with selected image to print at http://mysite.com/test1.jpg";

foreach ($items as $key=>$item){
    $temp = explode(" ", $item);
    $items[$key] = array();
    $items[$key]['quantity'] = $temp[0];
    $items[$key]['product_id'] = $temp[1];
    $items[$key]['variation'] = $temp[3];
    $items[$key]['selectedImageURL'] = $temp[10];
}

print_r($items);

这些方面应该有所作为。当然,假设您的数量和变化保持不变。