nette / utils

🛠 Lightweight utilities for string & array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.
https://doc.nette.org/utils
Other
1.98k stars 147 forks source link

Strings::webalize($haystack, '-') generates duplicate minus #284

Closed janbarasek closed 2 years ago

janbarasek commented 2 years ago

Version: v3.2.7

Bug Description

Helper webalize with charlist parameter - generates duplicated minus in output:

Steps To Reproduce

$title = 'Title with - minus';

echo Strings::webalize($title); // title-with-minus
echo Strings::webalize($title, '-'); // title-with---minus

Expected Behavior

The output string should never contain two minus signs next to each other.

For example, when writing articles and an improperly chosen input string, it is possible to generate several minuses side by side.

Possible Solution

Add this regex:

$s = self::pcre('preg_replace', ['#-+#', '-', $s]);

Thanks.

dg commented 2 years ago

Could you send a pull request?

dg commented 2 years ago

Btw, what's the point of the minus character in charlist?

janbarasek commented 2 years ago

The minus sign could be inserted as a user input in the administration.

I think it should always be ensured that the function never returns two consecutive minus signs, even if it gets the wrong input.

dg commented 2 years ago

I think the current behaviour is correct.