繁体   English   中英

如何在PHP中删除字符串开头/结尾处的可能点?

[英]How to remove the probable dot at the beginning/end of string in PHP?

我尝试了这个:

echo preg_replace('/[^,,$]/', '', ',test,hi,');

但得到:

,,,

你的意思是

preg_replace('/^,|,$/', '', ',test,hi,');

在字符类[…] ,前导^表示取反,而$没有任何特殊含义。

您可以改用trim功能

trim(',test,hi,', ',');

preg_replace有点矫kill过正

$string = ',,ABCD,EFG,,,,';
$newString trim($string,',');
trim(',test,hi,',','); // echoes test,hi

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM