在添加到数组之前拆分字符串

时间:2014-12-04 10:06:34

标签: php arrays

IM目前正在提取一些文字并将其添加到数组中,如下所示:

    foreach($getthesp2 as $thesp)
            {
        //  $sp[] = $thesp->textContent;
            $sp[] =      $thesp->textContent;

        }

目前的输出是这样的:

  

SP是Bubbly Bailey(5/2),Burnt Cream(4/1),Pearl Noir(5/1),   优质艺术(6/1),温暖订单(8/1),虚幻女神(10/1),尼尔森' s   骄傲(12/1),趋势(12/1),Senora Lobo(25/1),? SP是Mossgo   (7/2),Aaranyow(4/1),Ecliptic Sunrise(5/1),First Rebellion(6/1),   无与伦比的(7/1),Go Charlie(8/1),Live Dangerously(12/1),   Studfarmer(12/1),Purford Green(16/1),

我需要它做的是取字符串并将其分解,然后将其添加到数组中,以便输出为:

  

SP是Bubbly Bailey(5/2)SP是烧焦霜(4/1)SP是珍珠黑(5/1),....

2 个答案:

答案 0 :(得分:1)

使用array_map();

$sp = array("The SP is Bubbly Bailey (5/2)", "Burnt Cream (4/1)", "Pearl Noir (5/1)", "Quality Art (6/1)", "Warm Order (8/1)", "Imaginary Diva (10/1)", "Nelson's Pride (12/1)", "Trending (12/1)", "Senora Lobo (25/1)", "? The SP is Mossgo (7/2)"," Aaranyow (4/1)", "Ecliptic Sunrise (5/1)"," First Rebellion (6/1)", "Incomparable (7/1)", "Go Charlie (8/1)", "Live Dangerously (12/1)"," Studfarmer (12/1)", "Purford Green (16/1)");

function add_string($sp)
{
    // clean string
    $sp = str_replace("? The SP is ", '', $sp);
    $sp = str_replace("The SP is ", '', $sp);
    // concatenation
        return("The SP is ".$sp);
}
$sp_new = array_map("add_string", $sp);
print_r($sp_new);

答案 1 :(得分:0)

    $tags = explode(',',$new);

foreach($tags as $key) {    
    if ($key !== "")
        {$sp[] = $key;}

}

我做了以上解决我的问题。