从数组或fpdf中删除不需要的文本

时间:2014-04-14 21:10:34

标签: php string preg-replace fpdf

我有一个sql查询从表中提取presenter字段。演示者字段位于数组中。所以我这样做:

//convert presenter data from array to string   
    $cleanpresenters = parse_ini_string($data['presenters'],TRUE);
    foreach ($cleanpresenters as $name=>$val1) 
    {
            $name = trim($name);
            $presenters[] = $name;
            $convertedPresenters .= "$name, {$val1['department']}, {$val1['institution']}\n";
    };  

将其转换为:

阵 (     [Doe,John undefined] =>排列         (             [middle_name] =>             [department] =>领导力研究             [机构] =>任何州立大学             [city] =>顿             [state] =>哦             [country] =>美国             [office_phone] => 123-456-11258             [cell_phone] =>             [email_address] => John.doe@anywhere.edu             [网站] =>         )

[Doe, Jane undefined] => Array
    (
        [middle_name] => 
        [department] => Leadership Studies
        [institution] => Any State University
        [city] => Dayton
        [state] => Oh
        [country] => United States
        [office_phone] => 123-45-7896
        [cell_phone] => 
        [email_address] => John.doe@anywhere.edu
        [website] => 
    )

到此:

Doe,John undefined,领导力研究,任何州立大学

Doe,Jane undefined,领导力研究,任何州立大学

输出回显和使用fpdf创建的pdf。我的问题是 - 如何消除(替换为空格)" undefined"从所有的字符串?我已经尝试过使用preg_replace,但我已经足够了,我无法正常使用它。我是在正确的轨道上还是完全离开基地?谢谢!

2 个答案:

答案 0 :(得分:0)

要替换php中的字符串,请尝试使用str_replace

$new_string =  str_replace("undefined"," ","Doe, John undefined, Leadership Studies, Any State University");

答案 1 :(得分:0)

您可以使用preg_replace

来完成此操作
$cleanpresenters = parse_ini_string($data['presenters'],TRUE);
foreach ($cleanpresenters as $name=>$val1) 
{
        $name = trim(preg_replace('/ undefined/i', '', $name));
        $presenters[] = $name;
        $convertedPresenters .= "$name, {$val1['department']}, {$val1['institution']}\n";
};

undefined之前有一个空格,但是如果它不会一直存在,你就可以把它拿出来。