Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>

like image 519
Pascal Boschma Avatar asked Feb 03 '26 16:02

Pascal Boschma


2 Answers

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'
like image 195
SOFe Avatar answered Feb 05 '26 06:02

SOFe


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;
?>
like image 37
Niklesh Raut Avatar answered Feb 05 '26 04:02

Niklesh Raut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!