Search code examples
phpstringpreg-replacefpdf

Removing unwanted text from array or fpdf


I have a sql query which pulls the presenter fields from the table. The presenter fields are in an array. So I do this:

//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";
    };  

which converts this:

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

[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] => 
    )

)

to this:

Doe, John undefined, Leadership Studies, Any State University

Doe, Jane undefined, Leadership Studies, Any State University

in both the output echo and the pdf created using fpdf. My question is - how do I eliminate (replace with space) the "undefined" from all the strings? I have tried using preg_replace, but I am enough of a newb that I can't get it to work correctly. Am I on the right track or completely off base? Thanks!


Solution

  • to replace a string in php, try use str_replace:

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