[英]PHP preg_replace
* strong text *i have a string like that "x", "x,y" , "x,y,h" i want to user preg replace to remove the commas inside the double qutations and return the string as * 强文本 *我有一个像“ x”,“ x,y”,“ x,y,h”这样的字符串,我想用用户preg替换以删除双引号内的逗号并以
"x", "xy" , "xyh" “ x”,“ xy”,“ xyh”
您可以只使用常规替换。
$mystring = str_replace(",", "", $mystring);
您不需要在这里preg_replace()
,并且尽可能避免它
$string = str_replace(',', '', $string);
I use the following, which I've found is generally faster than regexp for this type of replacement 我使用以下内容,对于这种类型的替换,我发现它通常比regexp快
$string = '"x", "x,y" , "x,y,h"';
$temp = explode('"',$string);
$i = true;
foreach($temp as &$value) {
// Only replace in alternating array entries, because these are the entries inside the quotes
if ($i = !$i) {
$value = str_replace(',', '', $value);
}
}
unset($value);
// Then rebuild the original string
$string = implode('"',$temp);
This would work fine: http://codepad.org/lq7I5wkd 这样可以正常工作: http : //codepad.org/lq7I5wkd
<?php
$myStr = '"x", "x,y" , "x,y,h"';
$chunks = preg_split("/\"[\s]*[,][\s]*\"/", $myStr);
for($i=0;$i<count($chunks);$i++)
$chunks[$i] = str_replace(",","",$chunks[$i]);
echo implode('","',$chunks);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.