PHPでの簡易文字列の一致の判定

PHPで、ある文字列の中に、特定の文字列が含まれているかを判定する際、完全一致なら、比較演算子 === で比較すればすぐですが、それ以外となると通常の比較演算では処理できないので、ereg()関数やpreg()関数などで処理することも多いと思います。

ただ、前方一致や後方一致、部分一致を行いたい場合は、それだけに正規表現を使うのもなんだかコストが高い。もっとシンプルに実装できないものか・・・。

例えば、JavaやC#でいうところのStringクラスのstartsWith()やendsWith()のようなことが出来れば・・・

という訳で、PHPで前方一致、後方一致、部分一致の比較を簡単に行う関数を作ってみました。



PHP:
  1. /**
  2. * 前方一致
  3. * $haystackが$needleから始まるか判定します。
  4. * @param string $haystack
  5. * @param string $needle
  6. * @return boolean
  7. */
  8. function startsWith($haystack, $needle){
  9.     return strpos($haystack, $needle, 0) === 0;
  10. }
  11.  
  12. /**
  13. * 後方一致
  14. * $haystackが$needleで終わるか判定します。
  15. * @param string $haystack
  16. * @param string $needle
  17. * @return boolean
  18. */
  19. function endsWith($haystack, $needle){
  20.     $length = (strlen($haystack) - strlen($needle));
  21.     // 文字列長が足りていない場合はFALSEを返します。
  22.     if($length <0) return FALSE;
  23.     return strpos($haystack, $needle, $length) !== FALSE;
  24. }
  25.  
  26. /**
  27. * 部分一致
  28. * $haystackの中に$needleが含まれているか判定します。
  29. * @param string $haystack
  30. * @param string $needle
  31. * @return boolean
  32. */
  33. function matchesIn($haystack, $needle){
  34.     return strpos($haystack, $needle) !== FALSE;
  35. }



上記コードの検証結果は以下を参照



検証用コード (test.php)

PHP:
  1. <?php
  2. $target = array(
  3.     'mfsmax.docomo.ne.jp',
  4.     'lsean.ezweb.ne.jp',
  5.     'mx.softbank.ne.jp',
  6.     'mail.disney.ne.jp',
  7.     'mx.mailsv.softbank.jp',
  8.     'mail2.pdx.ne.jp',
  9.     'mailmlb.emnet.ne.jp',
  10.     'ne.jp',
  11.     '.ne.jp',
  12.     'ne.jp.ne.jp',
  13.     'foo.ne.jp.bar.net',
  14. );
  15.  
  16. $search = '.ne.jp';
  17.  
  18. print "n";
  19. print 'SIMPLE STRING PATTERN MATCHING TEST' . "\n";
  20. print "***********************************\n";
  21. foreach($target as $str){
  22.     print $str . "\n";
  23.     print 'startsWith: ';
  24.     print startsWith($str, $search) ? 'true' : 'false'; 'false';
  25.     print "\n";
  26.     print 'endsWith: ';
  27.     print endsWith($str, $search) ? 'true' : 'false'; 'false';
  28.     print "\n";
  29.     print 'matchesIn: ';
  30.     print matchesIn($str, $search) ? 'true' : 'false'; 'false';
  31.     print "\n";
  32.     print "-----------------------\n";
  33. }
  34.  
  35.  
  36.  
  37. function startsWith($haystack, $needle){
  38.     return strpos($haystack, $needle, 0) === 0;
  39. }
  40. function endsWith($haystack, $needle){
  41.     $length = (strlen($haystack) - strlen($needle));
  42.     if($length <0) return FALSE;
  43.     return strpos($haystack, $needle, $length) !== FALSE;
  44. }
  45. function matchesIn($haystack, $needle){
  46.     return strpos($haystack, $needle) !== FALSE;
  47. }



実行結果

$ php -q test.php

SIMPLE STRING PATTERN MATCHING TEST
***********************************
mfsmax.docomo.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
lsean.ezweb.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
mx.softbank.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
mail.disney.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
mx.mailsv.softbank.jp
startsWith: false
endsWith: false
matchesIn: false
-----------------------
mail2.pdx.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
mailmlb.emnet.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
ne.jp
startsWith: false
endsWith: false
matchesIn: false
-----------------------
.ne.jp
startsWith: true
endsWith: true
matchesIn: true
-----------------------
ne.jp.ne.jp
startsWith: false
endsWith: true
matchesIn: true
-----------------------
foo.ne.jp.bar.net
startsWith: false
endsWith: false
matchesIn: true
-----------------------


No TweetBacks yet. (Be the first to Tweet this post)

Related posts:

  1. PreparedなINSERT文を簡単に作る方法 PHPでWebアプリケーションなどを開発していて、SQL文を発行する際に、セキュア面や利便性などから、ADODBやPDOなどを用いて、Prepared Statementを使うSQLを書くこともあると思います。 その [...]...

関連記事はYARPP関連記事プラグインによって表示されています。

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes