Pascal Boschma
Pascal Boschma

Reputation: 197

Replace pairs of double-asterisks with opening and closing <strong> tags

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

Answers (2)

SOFe
SOFe

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

Niklesh Raut
Niklesh Raut

Reputation: 34924

You can also do like this

https://eval.in/518881

<?php
    $string = '**This Should be Bold**';
    $string = preg_replace("/\*\*(.+?)\*\*/", "<strong>$1</strong>", $string);
    echo $string;
?>

Upvotes: 1

Related Questions