Search code examples
phpstringtrim

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


For example, I have the following string:

$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>';

Is there a way to remove <p>&nbsp;</p> from the string if it appears consecutively (say more than 5 times in a row)?

I am looking for something like this:

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


Solution

  • Yes, without using regular expressions:

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

    So it replaces any double <p>&nbsp;</p> by singular <p>&nbsp;</p> until none are found. Doing five, if that's what you want, is as easy.