Skip to content

Conversation

@SirLouen
Copy link

@SirLouen SirLouen commented Dec 9, 2025

We were commenting in the WordPress repository about a strange condition: when running an empty string check vs. an empty string with strip_tags the underperforming difference was significant, so I decided to take a look into the code to find that there was no early return for this condition.

So I believe this could be a nice little upgrade to the function.

@kamil-tekiela
Copy link
Member

I don't mind this optimisation, although I wonder if this is the most common case. It will probably worsen performance in most cases.

A little off-topic, but are you sure that you want to use strip_slashes? It's a pretty strange function to use in production code.

@SirLouen
Copy link
Author

A little off-topic, but are you sure that you want to use strip_slashes? It's a pretty strange function to use in production code.

You mean stripslashes?

Here is strip_tags which commonly used to check and shortcircuit in anythin HTML parsing related.

@kamil-tekiela
Copy link
Member

I meant strip_tags. It's not wrong to use it, but it is a pretty naive solution. This function doesn't understand HTML and is just a very specific form of a regex. As we all know, HTML cannot be parsed with regex. It works for 99% of what people want it for, but in a product like WordPress, I am not sure if it's the best choice.

After having read the original bug report, I think it may actually work fine for what you want it to do, but whenever I see people use this function, it raises a red flag for me that there probably exists a better solution. What if a plugin adds this HTML formatting to an empty title:

<b>Title: </b>

where the actual title would go after the colon. What if the title is a topic about PHP:

<?php //Blog about writing PHP code

@SirLouen
Copy link
Author

I meant strip_tags. It's not wrong to use it, but it is a pretty naive solution. This function doesn't understand HTML and is just a very specific form of a regex. As we all know, HTML cannot be parsed with regex. It works for 99% of what people want it for, but in a product like WordPress, I am not sure if it's the best choice.

where the actual title would go after the colon. What if the title is a topic about PHP:

Nice catch. I will report it as it definitely should be considered.

Anyway, even if it was a valid use case or legit scenario where tags should be removed, the empty string is certainly the base scenario, so I think that early returning here is a little win, especially for how much allocation is required even for this base case scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants