Пример #1
0
 /**
  * Create a table object from a block of text.
  *
  * @param string $text  table definition
  * @return T_Text_Table  table object
  */
 protected function populateTable($text, T_Text_Table $table)
 {
     $lines = preg_split('/(?:\\r\\n|\\n|\\x0b|\\r|\\f|\\x85)\\s*/u', $text);
     // also removes starting spaces
     // starting delimiter has already been stripped by the parsing regex, so we just need
     // to check whether the auto header 2nd line exists, and then parse text into table data array.
     $is_headers = count($lines) > 1 && strncmp($lines[1], '|', 1) !== 0;
     $data = array();
     foreach ($lines as $row) {
         if (strncmp($row, '|', 1) !== 0) {
             continue;
         }
         // skip delimiters
         $data[] = explode('|', mb_trim(mb_trim($row), '|'));
     }
     // now we need to actually flesh out the data into a table. Here we need to account for the
     // fact that trailing empty elements in the array are merged via span into the previous cell.
     $per_row = max(array_map('count', $data));
     foreach ($data as $num => $src) {
         $table->addChild($row = new T_Text_TableRow());
         if (count($src) != $per_row) {
             // add extra cols at end which will be merged by span into previous cell
             for ($i = 0, $max = $per_row - count($src); $i < $max; $i++) {
                 $src[] = null;
             }
         }
         foreach ($src as $n => $cell) {
             if ($n > 0 && strlen($cell) == 0) {
                 continue;
             }
             // skip cells already created into spans
             $type = $is_headers || strncmp($cell, '^', 1) === 0 ? T_Text_TableCell::HEADER : T_Text_TableCell::PLAIN;
             $cell = mb_trim(mb_ltrim($cell, '^'));
             $span = 1;
             while (++$n < $per_row && strlen($src[$n]) == 0) {
                 ++$span;
             }
             $row->addChild(new T_Text_TableCell($cell, $type, $span));
         }
         $is_headers = false;
         // turn off auto headers after first line
     }
     return $table;
 }
Пример #2
0
<!DOCTYPE html>
<html lang="ja">
<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);
}
Пример #3
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);
    }
}
Пример #4
0
 function testLtrimLinefeedMask()
 {
     $str = "ñ\nñtërnâtiônàlizætiøn";
     $trimmed = "tërnâtiônàlizætiøn";
     $this->assertSame(mb_ltrim($str, "ñ\n"), $trimmed);
 }