Search code examples
phptrim

Strip commas, whitespace, periods from string in php


Users submit names in two separate fields: firstname(author_fname) and last name (author_lname) Entries are sometimes Smith, John J. or John and Mary Smith, or john j. smith. I need to get all extraneous characters out of the submission, so the name comes up with just the words: smithjohnj or johnandmarysmith. Using trim, I was able to delete the white space.

But I would like to get the comma out as well. Ex:

$text = $results["author_lname"].$results["author_fname"];
$trimmed = trim($text);
var_dump($trimmed);
$Aname = $trimmed;

Solution

  • for this you can use the php method: strtr as explained here: http://php.net/manual/en/function.strtr.php

    In your case you could use strtr with your already trimmed string like this:

    $stripped = strtr($trimmed, array(',' => ''));
    

    This should do the trick.