PHP preg_replace to include ** xyz ** in <b> xyz </b>

I decided, for fun, to do something like markdowns. Thanks to my little experience with regular expressions in the past, I know how strong they are, so they will be what I need.

So, if I have this line:

Hello **bold** world 

How can I use preg_replace to convert it to:

  Hello <b>bold</b> world 

Am I guessing something like this?

  $input = "Hello **bold** world"; $output = preg_replace("/(\*\*).*?(\*\*/)", "<b></b>", $input); 
+4
source share
4 answers

Close

 $input = "Hello **bold** world"; $output = preg_replace("/\*\*(.*?)\*\*/", "<b>$1</b>", $input); 
+7
source

I believe there is a PHP package for rendering Markdown. Instead of skating yourself, try using an existing set of code that has been written and tested.

+2
source

Mmm, I think it might work

 $output = preg_replace('/\*\*(.*?)\*\*/', '<b>$1</b>', $input); 

You find all the sequences **something** , and then you replace the entire sequence found with the bold tag and inside it ( $1 ) with the first captured group (brackets in the expression).

+1
source
 $output = preg_replace("/\*\*(.*?)\*\*/", "<b>$1</b>", $input); 
0
source

All Articles