Preg_replace for a word ending with a colon

I tried looking at preg_replace, except that I have little experience with regular expressions.

What I'm trying to do is replace the part of the text ending with a colon and make it bold, then put a line break before and 2 line breaks after.

IE converts this text

title1: is some text here. title2: another piece of text.

to

**title1:**
is some text here.

**title2:**
another piece of text

I tried to do something like this ...

preg_replace("!\*(.*?)\*!$:","\<br \/\>\<strong\>!\*(.*?)\*!\<\/strong\>\<br \/\>",$text);

I can not make it work.

Can anyone help?

thank

Andy

+3
source share
3 answers

preg_replaceregexes need to be distinguished. So, firstly, try to include the regex in slashes. Secondly, I do not understand your regular expression. Something simple how it /(\w+):/should work.

preg_replace("/(\w+):/", "<br><br><b>$1</b><br>", $text);

@AndiLeeDavis :

$mtext = preg_replace("/\.?\s*([^\.]+):/", "<br><br><b>$1</b><br>", $text);
if (strcmp(substr($mtext, 0, 8), "<br><br>") == 0) $mtext = substr($mtext, 8);
+5
$text = 'title1: is some text here. title2: another piece of text.';

echo preg_replace('#(\w+:) (.+?\.) ?#', "<b>$1</b><br />$2<br /><br />", $text);

... :

<b>title1:</b><br />is some text here.<br /><br /><b>title2:</b><br />another piece of text.<br /><br />

+2
   $str = 'title1: is some text here. title2: another piece of text.';
   $str = preg_replace("/(\w+:)/", "\n\n**$1**\n", $str);
   echo $str;
0

All Articles