In PHP, regular expression to remove the pound sig

2019-07-03 02:47发布

In PHP, I want to remove the pound sign (#) from a hex string if it exists.

I tried the following:

$str = "#F16AD3";

//Match the pound sign in the beginning
if (preg_match("/^\#/", $str)) {
  //If it's there, remove it
  preg_replace('/^\#/', '', $str);
};

print $str;

But it didn't work. It prints out #F16AD3

How can I remove the pound only if it exists?

标签: php regex
7条回答
Root(大扎)
2楼-- · 2019-07-03 03:48

Why using preg_replace for this?

echo str_replace("#","",$color);
查看更多
登录 后发表回答