Comments (16)
- trim() removes only single bytes specified in $characters parameter and can't safely remove multibyte characters.
- 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.
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.
In what cases is this necessary?
from strings.
In what cases is this necessary?
I'm using it to sanitize user input data.
from strings.
Sanitize it from what exactly? Why exactly this characters set but not, for example, x0c
?
from strings.
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.
-
Which is faster:
trim()
with full symbols list orpreg_replace()
? -
Should this method has name
trim
or more specific?
from strings.
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.
It can be applied to utf8 and ascii strings, not only to utf8.
from strings.
@olegbaturin Will you undertake to do PR?
from strings.
What about ltrim()/rtrim()?
from strings.
I think need use another name... May be:
* `regexpTrim()` (I like it) * `pregTrim()` * `reTrim()`
?
But I not sure. May betrim
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.
developers may use regular
trim
Regular trim
can't be used to remove unicode symbols, unfortunately.
from strings.
So would you like to implement it?
from strings.
I think all trim
, ltrim
, rtrim
can be here.
from strings.
Done by #109
from strings.
Related Issues (20)
- Optimize Inflector HOT 3
- Use more strict parameter type in methods `toOrdinal()` and `normalize()` of `NumericHelper`
- Change default value of `$strict` parameter in `Inflector::toSnakeCase()` to `false`
- Incorrect pluralization of words ending in "ion"
- truncate begin HOT 1
- Inflector::slug() delimiter syntax (merged words)
- Dependabot can't resolve your PHP dependency files
- Inflector::slug(): empty replacement string
- Dependabot can't resolve your PHP dependency files
- truncateMiddle improvements HOT 8
- New methods reverse and containsAny HOT 3
- Documentation HOT 1
- Benchmark if mb_str_starts_with / mb_str_ends_with are worth it for PHP 8 HOT 4
- What's the purpose to support null value in startsWith? HOT 3
- New method `StringHelper::explode()` HOT 1
- Add function camelCaseToSnakeCase
- Add method for implode array with callable HOT 2
- Inflector makes wrong snake case transformation HOT 5
- update links
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strings.