Exemple #1
0
/**
 * Unicode aware replacement for trim.
 *
 * @see trim
 * @param string $str  string to trim
 * @param string $charlist  list of characters to trim
 * @return string  trimmed string
 */
function mb_trim($str, $charlist = '')
{
    if (strlen($charlist) == 0) {
        return trim($str);
    } else {
        return mb_ltrim(mb_rtrim($str, $charlist), $charlist);
    }
}
<head>
	<meta charset="UTF-8">
	<title>文字列の前後の半角空白文字を削除する</title>
</head>
<body>
	<div>
	<?php 
$text = ' abc 123 ';
echo '<pre>削除前の文字列[' . $text . ']</pre>';
echo '<ul>';
echo '<li><p>前後の空白を削除</p><pre>[';
echo mb_trim($text) . ']</pre></li>';
echo '<li><p>先頭の空白を削除</p><pre>[';
echo mb_ltrim($text) . ']</pre></li>';
echo '<li><p>末尾の空白を削除</p><pre>[';
echo mb_rtrim($text) . ']</pre></li>';
echo '</ul>';
// mb_trim()関数
// 文字列の前後の空白(全角スペース含む)を削除した文字列を返す。
function mb_trim($str)
{
    return mb_ereg_replace('\\A(\\s| )+(\\s| )+\\z', '', $str);
}
// mb_ltrim()関数
// 文字列の先頭の空白(全角スペース含む)を削除した文字列を返す。
function mb_ltrim($str)
{
    return mb_ereg_replace('\\A(\\s| )+', '', $str);
}
// mb_rtrim()関数
// 文字列の末尾の空白(全角スペース含む)を削除した文字列を返す。
Exemple #3
0
 /**
  * Parse doc block text.
  *
  * @param string $comment  doc block comment
  */
 function __construct($comment)
 {
     // normalise EOL
     $regex = '/(?:\\r\\n|\\n|\\x0b|\\f|\\x85)/';
     $comment = preg_replace($regex, EOL, $comment);
     // remove doc block line starters
     $regex = '#[ \\t]*(?:\\/\\*\\*|\\*\\/|\\*)?[ ]{0,1}(.*)?#';
     $comment = trim(preg_replace($regex, '$1', $comment));
     // parse out long and short descriptions & tags
     //   o short desc is first non blank line.
     //   o long desc follows
     //   o tags are preceded by an '@'
     $short = $long = null;
     $first_blank = false;
     $tags = array();
     $tag = new T_Pattern_Regex('/^\\@([a-z]+) *(.*)$/i');
     foreach (explode(EOL, $comment) as $line) {
         if (!$first_blank && strlen($line) == 0) {
             $first_blank = true;
             continue;
             // skip line
         }
         if (!$first_blank) {
             $short .= $line . EOL;
         } elseif ($tag->isMatch($line)) {
             $matches = $tag->getFirstMatch($line);
             $name = strtolower($matches[1]);
             if ($name) {
                 $content = mb_trim($matches[2]);
                 if (strcmp($name, 'param') === 0) {
                     $regex = new T_Pattern_Regex('/^([0-9a-z_]+(?:\\[\\])?) +\\$([0-9a-z_]+)( +.*)?$/i');
                     if ($regex->isMatch($content)) {
                         $match = $regex->getFirstMatch($content);
                         $desc = isset($match[3]) ? trim($match[3]) : null;
                         $tags[] = new T_Code_DocBlockParamTag($name, $match[1], $match[2], $desc);
                     }
                 } elseif (strcmp($name, 'return') === 0 || strcmp($name, 'var') === 0) {
                     $break = strpos($content, ' ');
                     if ($break === false) {
                         $type = $content;
                         $desc = null;
                     } else {
                         $type = mb_substr($content, 0, $break);
                         $desc = trim(mb_substr($content, $break + 1));
                     }
                     $tags[] = new T_Code_DocBlockTypeTag($name, $type, $desc);
                 } else {
                     $tags[] = new T_Code_DocBlockTag($name, $content);
                 }
             }
         } else {
             $long .= $line . EOL;
         }
     }
     $long = mb_trim($long, EOL);
     $short = mb_rtrim($short, EOL);
     if ($short) {
         $this->short = $short;
     }
     if ($long) {
         $this->long = $long;
     }
     $this->tags = $tags;
 }
Exemple #4
0
 function testRtrimLinefeedMask()
 {
     $str = "Iñtërnâtiônàlizætiø\nø";
     $trimmed = "Iñtërnâtiônàlizæti";
     $this->assertSame(mb_rtrim($str, "ø\n"), $trimmed);
 }