Page 1 of 1

Put quotes in the beginning and the end

Posted: Fri Apr 17, 2009 4:29 pm
by xionhack
Hello. I have a string. I want to check if that string has a comma. If it has then to put quotes at the beginning and the end of the string, if it doesnt, then just to save the string.

Re: Put quotes in the beginning and the end

Posted: Fri Apr 17, 2009 4:46 pm
by jazz090
try this:

Code: Select all

<?php
$string = "a string that has, comma";
if (preg_match("/,/", $string)){
    $string = "\"".$string."\"";
}
echo $string;
?>
outputs:

Code: Select all

"a string that has, comma"

Re: Put quotes in the beginning and the end

Posted: Fri Apr 17, 2009 6:52 pm
by php_east
i think using strpos is easier and faster.

Code: Select all

<?php
if (strpos(",", $string)) 
{
$string = '"'.$string.'"';
}
echo $string;
?>