danielstjules / Stringy

A PHP string manipulation library with multibyte support
MIT License
2.46k stars 216 forks source link

Why is matchesPattern() private? #126

Closed brad-jones closed 7 years ago

brad-jones commented 8 years ago

As per title, it would be nice to be able to do this:

if (s("string")->matchesPattern("pattern"))
{
    echo 'We found a match.';
}
danielstjules commented 8 years ago

You're right - though the current name is a bit verbose, and I'm not sure what I'd name it. Calling it match might not be appropriate, since it returns a boolean rather than the regexp matches (preg_match in PHP, String.prototype.match in JS, re.match in python)

brad-jones commented 8 years ago

preg_match doesn't return the matches, it fills them via the third parameter. You could have matchesPattern or whatever you want to call it do likewise.

So for example:

if (s("string")->matchesPattern("pattern", $matches))
{
    echo 'We found at least 1 match.';
    print_r($matches);
}
danielstjules commented 8 years ago

Oops, you're right. That's not exactly an API I'd like to be consistent with though.

danielstjules commented 8 years ago

Note that the behavior would need to be changed from using mb_ereg_match to mb_ereg, since mb_ereg_match imposes ^ (matching from start of str)

danielstjules commented 7 years ago

Closing for now. Would like to explore an alternative API.