• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Strong from to : only if there is no period

The only problem with this is that it makes text bold even if there is . before a  :

If the : is after a . then I don't want it to be bold.

Test it and you will see the problem.

  $str = '
	This should be bold: this should not be bold
	<p>Intro text: this is a test.</p>  
	<p>Hello world: How are you?</p>   
	<p>This text should not be bold because there is no colon. Test 123.</p>   
	<p>This text should not be bold because there a period before the colon. Test: 123.</p>   
	<p>Foobar: Zooph.</p>';
        $str = preg_replace('/(^|<p>)([^:<]+):/ims', '\1<strong>\2:</strong>', $str); 
  echo $str;

Open in new window

1 Solution
So try this code snippet. If you allways have . before a </p> you could also try the second line
$str = preg_replace('/(^|<p>)([^:<.]+):/ims', '\1<strong>\2:</strong>', $str); 
$str = preg_replace('/(^|<p>)([^:.]+):/ims', '\1<strong>\2:</strong>', $str); 

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now