当前位置: 动力学知识库 > 问答 > 编程问答 >

php - How to find strings and add these string to an array

问题描述:

Anyone please help me..

$description = "This is product description. Quality is good. Title:title Price:1000 Size:XL, Medium, Small Color:red, green, blue. Any one can buy freely. ";

I wanna find "Title:", "Price:", "Size:" and "Color:" from that string and I want to add those values in an array.

My desired output is:

$new_desc = array(

'title'=>'title',

'price'=>1000,

'size'=>array(

[0]=>'XL',

[1]=>'Medium',

[2]=>'Small',

),

'color'=>array(

[0]=>'red',

[1]=>'green',

[2]=>'blue',

),

);

Thanks a lot!!!

网友答案:

Using preg_match_all, you can find you your identifiers (Price, Size ...) and their Values. You then just have to alter that to fit your array form.

Read up on preg_match_all and Regular Expressions.

<?php

$string = "This is product description. Quality is good. Title:title Price:1000 Size:XL, Medium, Small  Color:red, green, blue. Any one can buy freely. ";
preg_match_all('/([^ ]{1,}):(([0-9a-z]{1,}|([0-9a-z,]{1,})( ))+)/i', $string, $matches);
var_dump($matches);

So this gives you:

array(6) {
  [0]=>
  array(4) {
    [0]=>
    string(11) "Title:title"
    [1]=>
    string(10) "Price:1000"
    [2]=>
    string(22) "Size:XL, Medium, Small"
    [3]=>
    string(22) "Color:red, green, blue"
  }
  [1]=>
  array(4) {
    [0]=>
    string(5) "Title"
    [1]=>
    string(5) "Price"
    [2]=>
    string(4) "Size"
    [3]=>
    string(5) "Color"
  }
  [2]=>
  array(4) {
    [0]=>
    string(5) "title"
    [1]=>
    string(4) "1000"
    [2]=>
    string(17) "XL, Medium, Small"
    [3]=>
    string(16) "red, green, blue"
  }
  [3]=>
  array(4) {
    [0]=>
    string(5) "title"
    [1]=>
    string(4) "1000"
    [2]=>
    string(5) "Small"
    [3]=>
    string(4) "blue"
  }
  [4]=> [...]
}

if you iterate over the first set of matches from this array, you can create your desired output easily using stripos to find occurances of , and explode to generate arrays from your , seperated values.

$new_desc = array();
foreach ($matches[1] as $index => $identifier) {
  $value = $matches[2][$index];
  if(stripos($value, ',') !== FALSE) {
    $value = explode(',',$value);
  }
  $new_desc[$identifier] = $value;
}
var_dump($new_desc);

The full working DEMO can be found here.

网友答案:

Use preg_match_all() to extract the key value pairs first:

preg_match_all('/[A-Z][a-z]+:[a-z\d, ]+/', $description, $matches);

Then loop through the $matches array and use explode() to create your result array:

foreach ($matches[0] as $value) {
    list($key, $qty) = explode(':', $value);

    if (strpos($qty, ',') !== FALSE) {
        $result[strtolower($key)] = array_map('trim', explode(',', $qty));
    } else {
        $result[strtolower($key)] = trim($qty);
    }
}

var_dump($result);

Output:

array(3) {
  ["title"]=>
  string(5) "title"
  ["price"]=>
  string(4) "1000"
  ["color"]=>
  array(3) {
    [0]=>
    string(3) "red"
    [1]=>
    string(5) "green"
    [2]=>
    string(4) "blue"
  }
}

Demo

分享给朋友:
您可能感兴趣的文章:
随机阅读: