Coder Social home page Coder Social logo

add extended trim about strings HOT 16 CLOSED

yiisoft avatar yiisoft commented on May 16, 2024 1
add extended trim

from strings.

Comments (16)

olegbaturin avatar olegbaturin commented on May 16, 2024 1
  1. trim() removes only single bytes specified in $characters parameter and can't safely remove multibyte characters.
  2. When PCRE modifier u(PCRE_UTF8) is set, strings are treated as UTF-8, so it's a utf8 trim.

Generalized version

public static function trim(string $str, string $pattern = "\pC\pZ"): string
{
    return preg_replace("#^[$pattern]+|[$pattern]+$#u", '', $str);
}

from strings.

olegbaturin avatar olegbaturin commented on May 16, 2024 1

I think need use another name... May be:

* `regexpTrim()` (I like it)

* `pregTrim()`

* `reTrim()`

?

But I not sure. May be trim better name =) Need more opinions.

trim() is well known function name for string trimming. Does the implementation details matter?

from strings.

vjik avatar vjik commented on May 16, 2024

In what cases is this necessary?

from strings.

olegbaturin avatar olegbaturin commented on May 16, 2024

In what cases is this necessary?

I'm using it to sanitize user input data.

from strings.

samdark avatar samdark commented on May 16, 2024

Sanitize it from what exactly? Why exactly this characters set but not, for example, x0c?

from strings.

olegbaturin avatar olegbaturin commented on May 16, 2024

Updated method for advanced trimming ascii and utf8 strings

public static function trim(string $str): string
{
    return preg_replace("#^[\pC\pZ]+|[\pC\pZ]+$#u", '', $str);
}

List of unicode whitespace characters https://en.wikipedia.org/wiki/Whitespace_character#Unicode

from strings.

vjik avatar vjik commented on May 16, 2024
  1. Which is faster: trim() with full symbols list or preg_replace() ?

  2. Should this method has name trim or more specific?

from strings.

vjik avatar vjik commented on May 16, 2024

I think need use another name... May be:

  • regexpTrim() (I like it)
  • pregTrim()
  • reTrim()

?

But I not sure. May be trim better name =) Need more opinions.

from strings.

olegbaturin avatar olegbaturin commented on May 16, 2024

It can be applied to utf8 and ascii strings, not only to utf8.

from strings.

vjik avatar vjik commented on May 16, 2024

@olegbaturin Will you undertake to do PR?

from strings.

olegbaturin avatar olegbaturin commented on May 16, 2024

What about ltrim()/rtrim()?

from strings.

xepozz avatar xepozz commented on May 16, 2024

I think need use another name... May be:

* `regexpTrim()` (I like it)

* `pregTrim()`

* `reTrim()`

?
But I not sure. May be trim better name =) Need more opinions.

trim() is well known function name for string trimming. Does the implementation details matter?

The implementations matter when performance is a goal, but otherwise developers may use regular trim, so it's good to have kind of boosted trim

from strings.

olegbaturin avatar olegbaturin commented on May 16, 2024

developers may use regular trim

Regular trim can't be used to remove unicode symbols, unfortunately.

from strings.

xepozz avatar xepozz commented on May 16, 2024

So would you like to implement it?

from strings.

xepozz avatar xepozz commented on May 16, 2024

I think all trim, ltrim, rtrim can be here.

from strings.

vjik avatar vjik commented on May 16, 2024

Done by #109

from strings.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.