0

如何将此字符串替换*#ff00ff Hello World *000000<span style='color:#ff00ff'> Hello World </span>使用 str_replace?

谢谢。

4

2 回答 2

2
$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;
于 2012-11-27T07:59:42.073 回答
0
$str = '*#ff00ff Hello World *000000';
preg_match('/(#[0-9a-f]{6})/i', $str, $matches);
echo "<span style='color:{$matches[1]}'> Hello World </span>";
于 2012-11-27T08:10:30.823 回答