文字列の八方向検索
Posted feedbacks - PHP
$codeと同じエンコードでファイル保存。
有効な方向にだけ文字判別。
有効な方向にだけ文字判別。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | $code = "EUC-JP";
$data = "
リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ
";
$searches = array( 'ウ', 'オ', 'リ' );
header("Content-Type: text/html; charset=$code");
$search_count = count( $searches );
$lines = split( "\n", trim($data) );
$cols = mb_strlen( $lines[0], $code );
$rows = count( $lines );
$directions = array(
array( "direction" => "上", "col" => 0, "row" => -1 ),
array( "direction" => "下", "col" => 0, "row" => 1 ),
array( "direction" => "左", "col" => -1, "row" => 0 ),
array( "direction" => "右", "col" => 1, "row" => 0 ),
array( "direction" => "左上", "col" => -1, "row" => -1 ),
array( "direction" => "右上", "col" => 1, "row" => -1 ),
array( "direction" => "左下", "col" => -1, "row" => 1 ),
array( "direction" => "右下", "col" => 1, "row" => 1 )
);
foreach ( $lines as $key => $val ) {
$pos = 0;
while ( $pos !== false ) {
$pos = mb_strpos( $val, $searches[0], $pos, $code );
if ( $pos !== false ) {
foreach ( $directions as $row ) {
if ( $key + $row['row'] * ( $search_count - 1 ) >= 0
&& $key + $row['row'] * ( $search_count - 1 ) < $rows
&& $pos + $row['col'] * ( $search_count - 1 ) >= 0
&& $pos + $row['col'] * ( $search_count - 1 ) < $cols ) {
$check = 1;
for ( $i = 1; $i < count($searches); $i++ ) {
$search_row = $key + $row['row'] * $i;
$search_col = $pos + $row['col'] * $i;
if ( mb_substr( $lines[$search_row], $search_col, 1, $code ) != $searches[$i] ) {
break;
}
$check++;
}
if ( $check == $search_count ) {
echo("(" . $pos . "," . $key . ")," . $row['direction'] . "\n");
}
}
}
$pos++;
}
}
}
|


kuromin #4400() Rating0/2=0.00
[ reply ]