Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Remove text that is bold

Tags:

regex

text

php

I have some text for ex: This is <b>an</b> example <b>text</b>.

How would I remove all the text that is within bold tags, so it should output this:

This is example.

like image 538
sliceruk Avatar asked Apr 18 '26 00:04

sliceruk


1 Answers

Use preg_replace()
If you would like to remove a simple tag along with the text inside it:

<?php 
$string = 'This is <b>an</b> example <b>text</b>';
echo preg_replace('/(<b>.+?)+(<\/b>)/i', '', $string); 

Output:
This is example


And whit a regular expression (class, id), spacing errors and antislashe:

<?php
$string = 'This is <b class="c1" id=\'c2\'>an</b> example <b>text</B >'; 
echo preg_replace('@<(\w+)\b.*?>.*?</\1.*?>@si', '', $string); 

Output:
This is example

like image 176
Julien Avatar answered Apr 20 '26 14:04

Julien



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!