猿问

如何在文本中插入“,”[php]

我想在文本中插入“,”
我收到的文本如下:

A1780G1100001 A1780G1100002 A1780G1100003

当我显示文本时,我想这样显示:

A1780G1100001,A1780G1100002,A1780G1100003,

有哪些方法和代码?


慕桂英546537
浏览 90回答 1
1回答

呼唤远方

JS使用替换功能。let str='A1780G1100001 A1780G1100002 A1780G1100003';str = str.replace(/\s+/g, ',');console.log(str); // "A1780G1100001,A1780G1100002,A1780G1100003"PHP使用str_replace或preg_replace$str = 'A1780G1100001 A1780G1100002 A1780G1100003';$res = str_replace(' ', ',', $str);echo $res; // "A1780G1100001,A1780G1100002,A1780G1100003"echo "\n";$res = preg_replace('/\s+/', ',', $str);echo $res; // "A1780G1100001,A1780G1100002,A1780G1100003"
随时随地看视频慕课网APP
我要回答