简体   繁体   中英

PHP - Remove text that is bold

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.

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

Simple solution:

echo str_replace(array('<b>', '</b>'), '', 'This is <b>an</b> example <b>text</b>');

There may be better techniques. Here I just replace every occurence of the sequences in the array with an empty character. For more info, see php.net . They have a similiar example there:

// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");

Edit. I missed the regex tag, but regex is a teeny bit overkill for this purpose?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM