Put quotes in the beginning and the end
Moderator: General Moderators
Put quotes in the beginning and the end
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
try this:
outputs:
Code: Select all
<?php
$string = "a string that has, comma";
if (preg_match("/,/", $string)){
$string = "\"".$string."\"";
}
echo $string;
?>Code: Select all
"a string that has, comma"Re: Put quotes in the beginning and the end
i think using strpos is easier and faster.
Code: Select all
<?php
if (strpos(",", $string))
{
$string = '"'.$string.'"';
}
echo $string;
?>