user3047473
user3047473

Reputation: 105

PHP explode keywords but dont echo comma on the end

I need a list of keywords to split on 'keyword' but how to dont print the comma on the end, i know to count tags and to check if last tag and to dont display but im interesting in any other clean and good way.

Im using in this way:

$keywords = 'keyword1 keyword2 keyword3';
$keywords = explode(" ", $keywords);
foreach ($keywords as $keyword) {
echo "'" . $keyword . "', ";
}

this is printing:

'keyword1', 'keyword2', 'keyword3',

but i like to print without comma in the end, in this way:

'keyword1', 'keyword2', 'keyword3'

Upvotes: 1

Views: 742

Answers (7)

mickmackusa
mickmackusa

Reputation: 47992

Using array functions to prepare the string is not the most direct approach. Better practice would be to simply use str_replace() on the delimiting space and manually wrap the string in single quotes. One-line solution. No array functions, no iteration.

Code: (Demo)

$keywords = 'keyword1 keyword2 keyword3';
echo "'",str_replace(' ',"', '",$keywords),"'";

Output:

'keyword1', 'keyword2', 'keyword3'

This is the "clean" / "good" way.

Upvotes: 0

vijaykumar
vijaykumar

Reputation: 4806

try

 $str = '';
    $keywords = 'keyword1 keyword2 keyword3';
    $keywords = explode(" ", $keywords);
    foreach ($keywords as $keyword) {
    $str .= "'" . $keyword . "',";
    } 
    echo substr($str,0,-1);

rtrim($string, ",") would cut trailing commas.

trim($string, ",") would cut trailing and prefixing commas.

substr($yourstring, 0, -1); //last character.

Upvotes: -1

Maxime Lorant
Maxime Lorant

Reputation: 36171

You can implode every elements and print the result:

$keywords = 'keyword1 keyword2 keyword3';
$keywords = explode(" ", $keywords);
// Add single quotes
$keywords = array_map(function($v) { return "'".$v."'"; }, $keywords);
echo implode(", ", $keywords);

Which is the same as (without quotes):

$keywords = 'keyword1 keyword2 keyword3';
echo str_replace(' ', ', ', $keywords);

Upvotes: 3

monojit
monojit

Reputation: 605

$keywords = 'keyword1 keyword2 keyword3';
$keywords = explode(" ", $keywords);
$num =  count($keywords);
$count = 1;
foreach ($keywords as $keyword) {
if($count == $num){
    echo "'".$keyword."'" ;
} else {
    echo "'".$keyword ."',";
}   
$count++;
}

Upvotes: 0

CodeTwice
CodeTwice

Reputation: 2936

If I had to do this, I would use a combination of explode, implode and array_map, something like:

$keywords = 'keyword1 keyword2 keyword3';
$arrayOfKeywords = explode(' ', $keywords);
$mapped = array_map(function($value) {
    return "'{$value}'";
}, $arrayOfKeywords);

echo implode(', ', $mapped);

Upvotes: 2

jaco
jaco

Reputation: 3516

You could use implode:
You can try something like:

<?php
$keywords = 'keyword1 keyword2 keyword3';
$keywords = explode(" ", $keywords);
echo "'". implode("', '", $keywords) . "'";

Upvotes: 1

Moeed Farooqui
Moeed Farooqui

Reputation: 3622

$keywords = 'keyword1 keyword2 keyword3';
$keywords = explode(" ", $keywords);
$count = 1;
$comma = ',';
foreach ($keywords as $keyword) {
if($count == 3){
    $comma = '';
}
    echo "'" . $keyword .$comma;
$count++;
}

Upvotes: -1

Related Questions