I want to remove quotes from specific fields where there is an integer between the quotes.
From this line : "AAA","99"
to this line : "AAA",99
This is my string :
$string='"name","99","email","112"';
I want to remove quotes from specific fields where there is an integer between the quotes.
From this line : "AAA","99"
to this line : "AAA",99
This is my string :
$string='"name","99","email","112"';
1) Split by ',' into a table. (I'm using string_split, which is supported in MSSQL 2016+ - there are many ways to implement split functions in earlier SQL versions.)
2) Use REPLACE(value, '"', '') and ISNUMERIC
to figure out which values are numbers.
3) Use FOR XML PATH/STUFF to add the commas back.
DECLARE @str VARCHAR(25) = '"name","99","email","112"'
SELECT STUFF(
(
SELECT ',' +
CASE
WHEN ISNUMERIC(REPLACE(value, '"', '')) = 1
THEN REPLACE(value, '"', '')
ELSE value
END
FROM STRING_SPLIT(@str, ',')
FOR XML PATH('')
), 1, 1, '')
Returns:
"name",99,"email",112
$string = '"name","99","email","112"';
$string = str_replace('"', '', $string);
$strings = explode(",", $string);
foreach($strings as $key => $string) {
$strings[$key] = preg_match('/^[0-9]*$/', $string)
? intval($string) : '"'.$string.'"';
}
$string = implode(",",$strings)