This Content is from Stack Overflow. Question asked by Nuno
Consider this string (notice the horizontal scroll – the string is long):
$content = 'Xxxxxx xx xxxx xxxxxx/xxxx xxxxxxx xx xxxxx xx xxx XXXXXXX/XXXXX XXXX XXXXXXX XXXX XXXXXX XXXXX XXXXXX XXXXXX XXXXXX XXXXX XXXXXX';
I have my own
mb_trim() function to support unicode strings, but I found it’s performing really bad for this string in specific.
After debugging, I realized that it’s just the “end-of-string” bit that doesn’t perform, while “beginning-of-string” is fine.
So, just doing this (minimal code):
$trim = preg_replace('/s+$/u', '', $content);
This takes 2s ~ 3s.
But even without the
u modifier, it still takes ~1.60s.
If I replace the spaces in the middle with some letter, the
preg_replace will take 0s.
Is there a way to fix this performance issue?
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.