如何将此字符串替换*#ff00ff Hello World *000000
为<span style='color:#ff00ff'> Hello World </span>
使用 str_replace?
谢谢。
如何将此字符串替换*#ff00ff Hello World *000000
为<span style='color:#ff00ff'> Hello World </span>
使用 str_replace?
谢谢。
$string = '*#ff00ff Hello World *000000';
$string = preg_replace('/\*#([a-f\d]{6})(.*)\*[a-f\d]+/', "<span style='color:$1'>$2</span>", $string);
echo $string;
也可以这样做:
$string = '*#ff00ff Hello World *000000';
$string = preg_replace('/\*#([[:xdigit:]]{6})(.*)\*[[:xdigit:]]+/', "<span style='color:$1'>$2</span>", $string);
echo $string;
$str = '*#ff00ff Hello World *000000';
preg_match('/(#[0-9a-f]{6})/i', $str, $matches);
echo "<span style='color:{$matches[1]}'> Hello World </span>";