0

how do you replace this string *#ff00ff Hello World *000000 to <span style='color:#ff00ff'> Hello World </span> using str_replace?

Thanks.

2
  • 7
    $string = str_replace('*#ff00ff Hello World *000000', '<span style='color:#ff00ff'> Hello World </span', $string); Seriously, what are you trying to do? What have you tried and did not work? Commented Nov 27, 2012 at 7:57
  • Watch out ' and ". Thus would have to be $string = str_replace('*#ff00ff Hello World *000000', "<span style='color:#ff00ff'> Hello World </span>", $string); But good question where the original problem there is (thus what did not function with str_replace there) Commented Nov 27, 2012 at 8:00

2 Answers 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;

can also be done like so:

$string = '*#ff00ff Hello World *000000';

$string = preg_replace('/\*#([[:xdigit:]]{6})(.*)\*[[:xdigit:]]+/', "<span style='color:$1'>$2</span>", $string);

echo $string;
Sign up to request clarification or add additional context in comments.

Comments

0
$str = '*#ff00ff Hello World *000000';
preg_match('/(#[0-9a-f]{6})/i', $str, $matches);
echo "<span style='color:{$matches[1]}'> Hello World </span>";

1 Comment

oops, forgot the parenthesis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.