user2710234
user2710234

Reputation: 3225

PHP remove ', ' from string if doesn't exist

I am running this code:

$stmt = $pdo_conn->prepare("SELECT * from admin where support_emails = :support_emails and logged = :logged and disabled = :disabled ");
        $stmt->execute(array(':support_emails' => 'Y', ':logged' => 'in', ':disabled' => ''));
        $records = $stmt->fetchAll(PDO::FETCH_ASSOC);
        if(count($records) > 0) {
            foreach($records as $records2) {
                if(filter_var($records2["email"], FILTER_VALIDATE_EMAIL)) {
                    $SupportEmailList = $records2["email"].', '.$SupportEmailList;
                }
                if(!empty($SupportEmailList)) {
                    $SupportEmailList = substr($SupportEmailList, 0, -2); // removes last 2 characters (`, `) from end of string
                }
            }
        }

which returns

[email protected], [email protected]

when i run in SQL

i have added:

if(!empty($SupportEmailList)) {
                    $SupportEmailList = substr($SupportEmailList, 0, -2); // removes last 2 characters (`, `) from end of string
                }

to remove the ', ' from the end of the string if the last result doesn't exist, but it seems to be creating the following:

[email protected], [email protected]

rather than:

[email protected], [email protected]

Upvotes: 0

Views: 202

Answers (5)

voodoo417
voodoo417

Reputation: 12101

Do it:

$emails  = " [email protected] , fsdfsdffdsffds , [email protected],sddsfsdsd ";
$emails = array_map('trim',explode(',',$emails));

$out = array_map(function($email){
                return (filter_var($email, FILTER_VALIDATE_EMAIL)) ? $email : null;
             },$emails);

$emails = implode(',',array_filter($out));
print_r($emails);
// [email protected],[email protected]

Upvotes: 0

Ricbermo
Ricbermo

Reputation: 805

I don't understand your question, remove ',' if it doesn't exist? anyway You should fetch your data using PDO::FETCH_NUM. This way you'll get an array of emails like ['[email protected]', '[email protected]'] and then if you whant something like '[email protected], [email protected]' use implode to join the elements with a String.

Upvotes: 0

raj
raj

Reputation: 817

I would suggest keeping your results in an array and use implode (or join) to join them with a comma.

This way you don't have to deal with trailing comma issue.

$email_address = array();

foreach($records as $records2) {
    if(filter_var($records2["email"], FILTER_VALIDATE_EMAIL)) {
        array_push($email_address, $records2["email"]);
    }
}

$comma_separated_email_address = implode(",", $email_address);

echo $comma_separated_email_address ;

Upvotes: 6

Ruben Giaquinto
Ruben Giaquinto

Reputation: 390

You could split your string with explode and implode again

$data = explode(',', $elements);
$cleanedData = implode(',', $data);

You could trim space in your data trimming all values in $data array

for ($i = 0; $i < count($data); $i++)
    $data[$i] = trim($data[$i]);

Upvotes: 0

Sam
Sam

Reputation: 20486

Try trim(). You can pass a list of characters as a second parameter to choose what you want to trim from the beginning/end of the string:

$example_1 = '[email protected], [email protected]';
$example_2 = '[email protected], [email protected], ';

$example_1 = trim($example_1, ', ');
$example_2 = trim($example_2, ', ');

var_dump($example_1); // string(38) "[email protected], [email protected]"
var_dump($example_2); // string(38) "[email protected], [email protected]"

var_dump(explode(', ', $example_1));
// array(2) { [0]=> string(18) "[email protected]" [1]=> string(18) "[email protected]" }

Upvotes: 0

Related Questions