note 85333 modified in function.trim by felipe
| From: | felipe@php.net | Date: | Thu, 28 Aug 2008 13:41:16 +0000 |
| Subject: | note 85333 modified in function.trim by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143936@lists.php.net to get a copy of this message | ||
The following function will assist you in case you wish to trim a string but keep the parts trimmed.
I'm using it to strip phrases for translation on my site.
Example:
cleanPhrase(' (this () is it)');
Will return:
Array
(
[left] => (
[right] => )
[clean] => this () is it
)
<?php
/**
* Trims some of the characters from the phrase
*
* @param string $phrase - to be "cleaned"
* @param string $dirtyChars - the characters that need be removed
* @return string
*/
public static function cleanPhrase($phrase, $dirtyChars = " \t\n()[]:.\"'") {
$len = strlen($phrase);
for($i=0; $i < $len; $i++) {
$pos = strpos($dirtyChars, substr($phrase, $i, 1));
if ($pos === false) break;
}
for($j=$len-1; $j >= 0; $j--) {
$pos = strpos($dirtyChars, substr($phrase, $j, 1));
if ($pos === false) break;
}
$res = array();
$res['left'] = substr($phrase, 0, $i);
$res['right'] = substr($phrase, $j+1);
$res['clean'] = substr($phrase, $i, $j-$i+1);
return $res;
}
?>
--was--
The following function will assist you in case you wish to trim a string but keep the parts trimmed.
I'm using it to strip phrases for translation on my site.
Example:
cleanPhrase(' (this () is it)');
Will return:
Array
(
[left] => (
[right] => )
[clean] => this () is it
)
/**
* Trims some of the characters from the phrase
*
* @param string $phrase - to be "cleaned"
* @param string $dirtyChars - the characters that need be removed
* @return string
*/
public static function cleanPhrase($phrase, $dirtyChars = " \t\n()[]:.\"'") {
$len = strlen($phrase);
for($i=0; $i < $len; $i++) {
$pos = strpos($dirtyChars, substr($phrase, $i, 1));
if ($pos === false) break;
}
for($j=$len-1; $j >= 0; $j--) {
$pos = strpos($dirtyChars, substr($phrase, $j, 1));
if ($pos === false) break;
}
$res = array();
$res['left'] = substr($phrase, 0, $i);
$res['right'] = substr($phrase, $j+1);
$res['clean'] = substr($phrase, $i, $j-$i+1);
return $res;
}
http://php.net/manual/en/function.trim.php