如何使用 PHP 从字符串中删除过多的字符?

How to Remove Excessive Bunch of Characters From String Using PHP?

例如,我有以下字符串:

$description = '<p><span style="text-decoration: underline;"><strong>General</strong></span></p>
    <p>Protecting all workers from possible injuries associated with encountering aggressive animals.</p>
    <p>&nbsp;</p>
    <p><span style="text-decoration: underline;"><strong>Application</strong></span></p>
    <p>Employees must be vigilant in their awareness of potential animal encounters during field work.</p>
    <p><strong>&nbsp;</strong></p>
    <p><span style="text-decoration: underline;"><strong>Protective Mechanisms</strong></span></p>
    <p>PPE</p>
    <p>Specialized Equipment (i.e., bear bangers, bear spray, whistle or air horn)</p>
    <p>Safe Work Practice</p>
    <p><strong>&nbsp;</strong></p>
    <p><span style="text-decoration: underline;"><strong>Selection and Use</strong></span></p>
    <p>As per safe job requirements.</p>
    <p>Location and/or land use.</p>
    <p><strong>&nbsp;</strong></p>
    <p><span style="text-decoration: underline;"><strong>Supervisor/Owner Responsibility</strong></span></p>
    <ol>
    <li>Identify jobs with potential encounters, and inform workers. Complete a hazard analysis prior to initiation of any site work. Complete ERP and review with workers.</li>
    <li>Discuss procedures to prevent or avoid encounters.</li>
    <li>Provide necessary equipment to prevent or influence the outcome of encounters.</li>
    <li>Report all encounters to an appropriate authority (local Wildlife Officer).</li>
    </ol>
    <p><strong>&nbsp;</strong></p>
    <p><span style="text-decoration: underline;"><strong>Worker Responsibility</strong></span></p>
    <ol>
    <li>Follow procedures developed to avoid encounters.</li>
    <li>Make noise when moving through wooded areas.</li>
    <li>Be aware of the preferred habitat for various wildlife.</li>
    <li>Look and listen for animal activity (scat, grunts, and disturbed logs).</li>
    <li>Carry Bear spray, bear bangers or an air horn.</li>
    <li>Review the safe job procedures for using bear spray and bear bangers.</li>
    <li>Avoid eye contact or moving towards an animal.</li>
    <li>Back away slowly, facing animal (bear) stand tall, and make noise.</li>
    <li>If necessary, climb a tree.</li>
    <li>In pasture, be aware of livestock location when opening gates and moving through a field. If a bull is present, assess the safety of entering a field without a vehicle.</li>
    <li>Leave an area if you feel uncomfortable or unsafe and <strong>report to supervision.</strong></li>
    </ol>
    <p>&nbsp;</p>
    <p>Hazards:&nbsp; Dangerous wildlife can be defined as untamed animals of a predatory nature that have the ability to severely injure or kill people.&nbsp; Along with bears, this may also include stray dogs, aggravated moose, cougars, elk and deer, particularly during rutting season.</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>';

如果 <p>&nbsp;</p> 连续出现(比如连续出现超过 5 次),有没有办法从字符串中删除它?

我正在寻找这样的东西:

$description = preg_replace("/<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>+/", "", $description);

是的,不使用正则表达式:

$offending = '<p>&nbsp;</p>';
while (strpos($description,$offending.$offending) !== FALSE) {
  $description = str_replace($offending.$offending,$offending,$description);
}

所以它用单数 <p>&nbsp;</p> 替换任何双 <p>&nbsp;</p> 直到找到 none 。做五个,如果那是你想要的,那么简单。

您需要使用 {5,} 表示 5 次或更多次的修饰符

$description = preg_replace("/(<p>&nbsp;<\/p>){5,}/", "", $description);

您可以使用正则表达式和反向引用来实现它。以下将留下不超过 5 次 <p>&nbsp;</p>:

$string = "<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>";

//match 5 occurrences of <p>&nbsp;<\/p> followed by one or more occurrences of <p>&nbsp;<\/p>
$pattern = "/((<p>&nbsp;<\/p>){5})(<p>&nbsp;<\/p>)+/";

$replaced = preg_replace($pattern, '', $string );