1

I am new in PHP

I want to remove last comma of a string, how can i do that. Here is my code :

<?php
$sub ="economic,maths,science";
$cap = explode(",",$sub);
foreach($cap as $new){
    echo ucfirst($new).",";
    }
?>

any help would be greatly appreciated, thanks in advance.

Sunny
  • 21
  • 3
  • thnx to all your efforts, i appreciate it but i can't vote up because i don't have enough reputation. – Sunny Apr 29 '12 at 14:30

7 Answers7

2

simple trim would be enough:

$string = trim($string, " ,");

Note that the second parameter of trim() function allows you to trim defined characters from your string, not just the whitespace. Therefore there are two chars defined in my usage: The space character " " and the comma ",".

and if you look for capitalizing the words without a loop:

$string = ucwords(trim($string, " ,"));

Note: as ucwords() function looks for whitespace to define word boundaries, "apple,apple" won't work but "apple, apple" would work, so:

$string = ucwords(str_replace(array(","," "),array(", "," "),trim($string, " ,")));

is the best solution. (There are two spaces in the second element of first replacement array.)

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
1
<?php
$sub ="economic,maths,science";
$cap = explode(",",$sub);
$cap2 = array();
foreach($cap as $new){
    $cap2[] = ucfirst($new);
}
echo implode(",",$cap2)
?>
Sethunath K M
  • 4,702
  • 3
  • 28
  • 42
1
$sub ="economic,maths,science";
var_dump(implode(',', array_map('ucfirst', explode(",", $sub))));
Eugene
  • 3,280
  • 21
  • 17
0

This one works with word boundaries, not just commas:

preg_replace('~\b(\w+)\b~e', 'ucfirst("\\1")', "economic,maths,science");
guido
  • 18,864
  • 6
  • 70
  • 95
  • A regex with the e flag is particully slow, and you're using a regex in a situation which should have been handled with an `implode`. – sg3s May 05 '12 at 01:07
  • Have any links on e flag performance or any reason to prefer a specific piece of code over a general-purpose regex? – guido May 05 '12 at 02:30
  • I can't find the article on the performance problem but it had something to do with the add/strip slashed performed on strings. But even then [there are other problems to worry about](http://stackoverflow.com/questions/3115559/exploitable-php-functions). But that isn't the main problem here, starting the regex engine, doing a fairly match, which btw doesn't ensure everything between commas is used (there are other boundries), you're exposed to other problems when applying this to longer, more complex strings. – sg3s May 05 '12 at 10:22
-1

You can do this without a loop like so:

$string = 'test, test, test';
$pos = strrpos($string, ',');

$string[$pos] = '';

echo $string;
Tom Walters
  • 15,366
  • 7
  • 57
  • 74
-1

A simple regular expression would do:

preg_replace('/,([^,])$/','\1',$str);
d_inevitable
  • 4,381
  • 2
  • 29
  • 48
-2
<?php 
$sub = "economic,maths,science";
$cap = explode(",",$sub);
$count = count($cap);
$i = 1;     
foreach($cap as $newSub){
    if($count>$i){
        echo ucfirst($newSub).",";
    }else{
        echo ucfirst($newSub);
    }
    $i++;
}   
?>
Jassi Oberoi
  • 1,424
  • 1
  • 12
  • 25