在PHP中,我想从十六进制字符串中删除井号(#)(如果存在).
我尝试了以下方法:
$str = "#F16AD3";
//Match the pound sign in the beginning
if (preg_match("/^\#/", $str)) {
//If it's there, remove it
preg_replace('/^\#/', '', $str);
};
print $str;
Run Code Online (Sandbox Code Playgroud)
但它没有用.打印出来#F16AD3
如果它存在,我怎样才能删除它?
kar*_*m79 14
echo ltrim('#F16AD3', '#');
Run Code Online (Sandbox Code Playgroud)
http://php.net/manual/en/function.ltrim.php
编辑:如果您只是在字符串开头测试英镑符号,您可以使用strpos
:
if(strpos('#F16AD3', '#') === 0) {
// found it
}
Run Code Online (Sandbox Code Playgroud)
您必须将响应分配回变量:
$str = preg_replace('/^\#/', '', $str);
Run Code Online (Sandbox Code Playgroud)
此外,您根本不需要使用preg_match进行检查,这是多余的.