0

If you have many string functions on one string, what is the best thing to do?

$string = strip_tags(utf8_decode(html_entity_decode(str_replace(["\r\n\r\n", "<br />", "<br>", "</p>"], "\r\n", $string), ENT_QUOTES)));

This is a code snippit from my script, and I think it looks horrible, but I need all of these functions on this string... I use this only once, so putting it in a function is overkill imo.

If you have any suggestions, please let me know :)

P.s. still a beginner at PHP...

1
  • I think you're fine, any other solution will be a repetition, except you want something totally different Commented Jan 2, 2017 at 10:56

2 Answers 2

1

You can use proper indentation to enhance the readability of the code:

$string = strip_tags(
    utf8_decode(
        html_entity_decode(
            str_replace(
                [
                    "\r\n\r\n", 
                    '<br />', 
                    '<br>', 
                    '</p>'
                ], 
                "\r\n", 
                $string
            ), 
            ENT_QUOTES
        )
    )
);

But regardless of that I definitely would recommend to move such code into a separate function! That allows you to chose a name that describes what it actually does, so that you can keep the internal details of that logic out of the code where you apply it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I used your code in a function, and it indeed looks better, and most certainly more readable!
You are welcome. To keep things a bit more compact one could also keep the four patterns to be str_replace()ed in a single row, so ["\r\n\r\n", '<br />', '<br>', '</p>'].
1

I use this only once, so putting it in a function is overkill imo.

I totally do not second that. The intent of your code is anything but obvious and you would make your code way more readable - and hence maintainable - if you put the line in a method

function remove_all_tags($string)
{
    return strip_tags(utf8_decode(html_entity_decode(str_replace(["\r\n\r\n", "<br />", "<br>", "</p>"], "\r\n", $string), ENT_QUOTES)));
}

I'd furthermore suggest to separate the calls to make the intent clearer

function remove_all_tags($string)
{
    $stringWithUnifiedNewline = str_replace(["\r\n\r\n", "<br />", "<br>", "</p>"], "\r\n", $string);
    $htmlDecodedString = html_entity_decode($stringWithUnifiedNewline, ENT_QUOTES)
    $utf8DecodedString = utf8_decode($htmlDecodedString);
    return strip_tags($utf8DecodedString);
}

Now compare your original code

$string = strip_tags(utf8_decode(html_entity_decode(str_replace(["\r\n\r\n", "<br />", "<br>", "</p>"], "\r\n", $string), ENT_QUOTES)));

to

$string = remove_all_tags($string);

Your intent is clearer, the code cleaner and everyone's happy.

1 Comment

Thanks, I did not end up using your version since the answer I accepted is a bit more readable, even though it uses much more space. Your code is very clear in what it does though, +1 for that ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.