文字列検索 PHP 文字列一致
指定文字が文字列の中にどれだけあるかカウントするPHP関数。
substr_count( 文字列 , 検索文字 ) //文字列に検索文字が含まれる回数を返します。
例:文字列”ABCABCDEF”の中に文字列”BC”の出現回数を取得する場合。
<?php
print substr_count(”ABCABCDEF”,”BC”);
?>
●実行結果
2
トラックバック URL :
コメント (1681)指定文字が文字列の中にどれだけあるかカウントするPHP関数。
substr_count( 文字列 , 検索文字 ) //文字列に検索文字が含まれる回数を返します。
例:文字列”ABCABCDEF”の中に文字列”BC”の出現回数を取得する場合。
<?php
print substr_count(”ABCABCDEF”,”BC”);
?>
●実行結果
2
トラックバック URL :
コメント (1681)
/* $strはHTMLソースです */
function myStrlen($str){
// HTMLタグを削除
$str = strip_tags($str);
// 改行を削除
$str = preg_replace("/(\015\012)|(\015)|(\012)/", "", $str);
// 連続する半角スペースを半角スペース1としてカウント
$str = preg_replace('!\s+!', " ", $str);
// HTML特殊文字を半角1文字としてカウント
$str = preg_replace("/&[a-zA-Z]{1,5};/", " ", $str);
// Unicode10進文字を半角1文字としてカウント
$str = preg_replace("/&#[0-9]{1,5};/", " ", $str);
// PHPマルチバイト対応
if( function_exists('mb_strlen') ){
$result = mb_strlen($str,'utf-8');
}else{
$result = strlen($str);
}
return $result;
}
※注意:mb_strlen($str,”utf-8″)において、”utf-8″の指定がない場合、文字カウント数がおかしくなるので注意 。
・参考ソース
http://xoops.suinyeze.com/modules/bulletin/index-page-article-storyid-46.html
トラックバック URL :
コメント (7065)HTML convert time: 0.115 sec. Powered by WordPress ME