Skip to content

Commit

Permalink
Add PHPDoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayesh committed Jan 10, 2024
1 parent 7f0a46f commit 965c98f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/mb_trim.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?php

/**
* Multi-byte safely strip whitespace (or other characters) from the beginning and end of a string.
*
* @param string $string The string that will be trimmed.
* @param string $characters Optionally, the stripped characters can also be specified using the $characters parameter. Simply list all characters that you want to be stripped.
* @param string|null $encoding The encoding parameter is the character encoding.
*
* @return string The trimmed string.
*/
function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {
// On supported versions, use a pre-calculated regex for performance.
if (PHP_VERSION_ID >= 80200 && ($encoding === null || $encoding === "UTF-8") && $characters === " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}") {
return preg_replace("/^[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+|[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+$/u", '', $string);
}
Expand Down Expand Up @@ -33,7 +43,17 @@ function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u
return $return;
}

/**
* Multi-byte safely strip whitespace (or other characters) from the beginning of a string.
*
* @param string $string The string that will be trimmed.
* @param string $characters Optionally, the stripped characters can also be specified using the $characters parameter. Simply list all characters that you want to be stripped.
* @param string|null $encoding The encoding parameter is the character encoding.
*
* @return string The trimmed string.
*/
function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {
// On supported versions, use a pre-calculated regex for performance.
if (PHP_VERSION_ID >= 80200 && ($encoding === null || $encoding === "UTF-8") && $characters === " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}") {
return preg_replace("/^[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+/u", '', $string);
}
Expand Down Expand Up @@ -66,7 +86,17 @@ function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\
return $return;
}

/**
* Multi-byte safely strip whitespace (or other characters) from the end of a string.
*
* @param string $string The string that will be trimmed.
* @param string $characters Optionally, the stripped characters can also be specified using the $characters parameter. Simply list all characters that you want to be stripped.
* @param string|null $encoding The encoding parameter is the character encoding.
*
* @return string The trimmed string.
*/
function mb_rtrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string {
// On supported versions, use a pre-calculated regex for performance.
if (PHP_VERSION_ID >= 80200 && ($encoding === null || $encoding === "UTF-8") && $characters === " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}") {
return preg_replace("/[" . "\t\n\v\r\f\0\p{Z}\u{0085}\u{180E}" . "]+$/u", '', $string);
}
Expand Down

0 comments on commit 965c98f

Please sign in to comment.