Php 检测字符串是否只包含逗号,然后删除逗号
首先,我有一个这样的动态字符串 , 有时候像这样 名字 有时候也是这样 ,姓 如果字符串只包含逗号,如何检测like,然后使字符串为空(删除逗号),或者如果字符串包含firstname和comma(没有lastname,请参见下面的格式) 名字 然后删除字符串中的逗号并保留firstname,否则如果字符串包含逗号和lastname,则删除逗号并将lastname保留在字符串中Php 检测字符串是否只包含逗号,然后删除逗号,php,Php,首先,我有一个这样的动态字符串 , 有时候像这样 名字 有时候也是这样 ,姓 如果字符串只包含逗号,如何检测like,然后使字符串为空(删除逗号),或者如果字符串包含firstname和comma(没有lastname,请参见下面的格式) 名字 然后删除字符串中的逗号并保留firstname,否则如果字符串包含逗号和lastname,则删除逗号并将lastname保留在字符串中 //Here we are replacing all occurrences of "," with "" insi
//Here we are replacing all occurrences of "," with "" inside $string
$string = str_replace(",", "", $string);
有什么想法、帮助、线索、建议、建议吗?如果逗号仅位于字符串的开头和结尾,您可以这样做:
$firstname = rtrim('firstname', ',');
$lastname = ltrim(', lastname', ', ');
签出并如果逗号仅位于字符串的开头和结尾,则可以执行以下操作:
$firstname = rtrim('firstname', ',');
$lastname = ltrim(', lastname', ', ');
检查和
,可以用字符串替换字符串中所有出现的字符串,即使逗号位于字符串中间,也可以工作。//Here we are replacing all occurrences of "," with "" inside $string
$string = str_replace(",", "", $string);
如果您只想从左/右删除逗号,则trim()是一种方法,如下所示:
$string = trim($string, ",");
资料来源:
希望我能帮上忙 > p>可以用字符串替换字符串中所有出现的字符串,即使逗号位于字符串的中间,它也可以工作。
//Here we are replacing all occurrences of "," with "" inside $string
$string = str_replace(",", "", $string);
如果您只想从左/右删除逗号,则trim()是一种方法,如下所示:
$string = trim($string, ",");
资料来源:
希望我能帮上忙 你可以试试这个
<?php
$name = 'Firstname,'; // If you give value "," you will get blank
$filename_withou_comma = explode (',',$name);
echo $filename_withou_comma[0];
echo $filename_withou_comma[1];
?>
您可以试试这个
<?php
$name = 'Firstname,'; // If you give value "," you will get blank
$filename_withou_comma = explode (',',$name);
echo $filename_withou_comma[0];
echo $filename_withou_comma[1];
?>
也许$string=trim($string,,)代码>应该这样做。$str=str\U替换(“,”,“”,$str);Preg_replace也是一个选项,基本上总是删除逗号?你想不出更复杂的方法来表达这个词了吗?也许$string=trim($string,,)代码>应该这样做。$str=str\U替换(“,”,“”,$str);Preg_replace也是一个选项,基本上总是删除逗号?你想不出比这更复杂的方法了吗?wil,字符串是动态的,所以首先我必须从上面的场景中检测。是的,你可以使用动态字符串,例如,你可以从数据库或POST或GET调用它。wil,字符串是动态的,所以首先我必须从上面的场景中检测。是的,您可以使用动态字符串,例如,您可以从数据库、POST或GET调用它。wil,字符串是动态的,所以首先我必须从上面的场景中检测。如果它是动态的,并且您想要删除所有的逗号,那么使用str_replace.wil,字符串是动态的,所以首先我必须从上面的场景中检测。如果它是动态的,并且您想要删除所有的逗号,那么使用str_replace。