Reputation: 197
When I replace something, it will just replace everything; That's okay. But what I would like to know is this:
str_replace("*", "<strong>", $message);
Is it possible to use str_replace()
for codes like * This content is Bold *
, just having the content, but still replacing the asterisks with <strong>
and </strong>
?
Example:
Original: **This Should be Bold**
After Replacing: <strong>This Should be Bold</strong>
Upvotes: 3
Views: 381
Reputation: 8224
Use regular expression instead; it's more convenient:
$message = "**This Should be Bold**";
$message = preg_replace('#\**([^\*]+)\**#m', '<strong>$1</strong>', $message);
echo $message;
Or if you want to limit the number of asteroids to 2:
'#\*{1,2}([^\*]+)\*{1,2}#m'
Upvotes: 3
Reputation: 34924
You can also do like this
<?php
$string = '**This Should be Bold**';
$string = preg_replace("/\*\*(.+?)\*\*/", "<strong>$1</strong>", $string);
echo $string;
?>
Upvotes: 1