I have paragraphs of texts with this kind of markup in them:
text1 *text2* text3
I need to turn them into
text1 <strong>text2</strong> text3
I tried this code
$pattern = '~\*(.*)\*~';
$replacement = '<strong>\1</strong>';
$str = preg_replace($pattern, $replacement, $str);
It doesn't recognize the second, closing asterisk. The closing strong" html tag only appears at the next (third) asterisk much later in the same text.
What am I missing?
Update: Avinash's pattern worked for the issue, but here is a related one
I used the same pattern (the first of the two) to replace underscores with tags. But the same paragraphs included images with syntax like <img src="/my_image.jpg" width="700" height="300">
How do I avoid this regex turning those underscores? (The width/height numbers vary, not constant.)
E.g. from a text like this
Lorem ipsum dolor sit amet, <img src="/my_image.jpg" width="700" height="300"> adipiscing elit. _Fusce pharetra_ a dui nec interdum.
I would like to get this
Lorem ipsum dolor sit amet, <img src="/my_image.jpg" width="700" height="300"> adipiscing elit. <em>Fusce pharetra</em> a dui nec interdum.