aphextwix
aphextwix

Reputation: 1858

Unexpected result with PHP function to format string

I'm attempting to take an input string as follows:

Example Input

  <p>
    In spite of how delightfully smooth the full-grain leather may be,
    it still provides quite a bit of durability to the upper of this
    Crush by Durango women&#39;s Western boot. Everything about this
    pull-on boot is stunning; you have the floral embroidery, the pop
    of metallic and a stylish x toe. To make these 12&rdquo; floral
    boots ease to get on, Durango added two very sturdy pull straps
    next to the boot&rsquo;s opening.</p>
<ul>
    <li>
        2 1/4&quot; heel</li>
    <li>
        Composition rubber outsole with vintage finish</li>
    <li>
        Cushion Flex insole</li>
</ul>

And generate the following output:

Output String

In spite of how delightfully smooth the full-grain leather may be,
it still provides quite a bit of durability to the upper of this Crush by
Durango women's Western boot. Everything about this pull-on boot is stunning;
you have the floral embroidery, the pop of metallic and a stylish x toe.
To make these 12” floral boots ease to get on, Durango added two very sturdy
pull straps next to the boot’s opening.

2 1/4" heel
Composition rubber outsole with vintage finish
Cushion Flex insole

I have the following function :

Function

function cleanString($str)
{
    $content = '';

    foreach(preg_split("/((\r?\n)|(\r\n?))/", strip_tags(trim($str))) as $line) {
    $content .= " " . trim($line) . PHP_EOL;
    }

    return $content;
}

At the moment this function returns In spite of how delightfully smooth the full-grain leather may be and trims the remainder of the string.

Could anybody please explain how to alter the function to generate the output stated above?

Upvotes: 0

Views: 72

Answers (2)

Neal
Neal

Reputation: 3179

If I understand your requirement properly you can simply use strip_tags() function in php.

More here http://php.net/manual/en/function.strip-tags.php

Upvotes: 1

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

The issue you were having was becuase of preg_split(). This causes the string to "explode", on a match of the regular expression, meaning on the first part was to be returned only, and "trim" the rest.

Something like this should suffice

<?php

$s = "  <p>
    In spite of how delightfully smooth the full-grain leather may be,
    it still provides quite a bit of durability to the upper of this
    Crush by Durango women&#39;s Western boot. Everything about this
    pull-on boot is stunning; you have the floral embroidery, the pop
    of metallic and a stylish x toe. To make these 12&rdquo; floral
    boots ease to get on, Durango added two very sturdy pull straps
    next to the boot&rsquo;s opening.</p>
<ul>
    <li>
        2 1/4&quot; heel</li>
    <li>
        Composition rubber outsole with vintage finish</li>
    <li>
        Cushion Flex insole</li>
</ul>";

echo html_entity_decode( preg_replace("/<.+>/", "", trim($s) ) );

https://eval.in/198897

Upvotes: 0

Related Questions