LoginSignup
0
0

More than 3 years have passed since last update.

Preg_match関数

Posted at

こちらを参考に
正規表現によるマッチング

<?php

  $name = 'Name';
  $nickname = 'nickname';

  if (preg_match("/$name/i", $nickname)) {
    echo "OK";
  } else {
    echo "NO";
  } 

?>

$nameは$nicknameの中に含まれるのか
検証

$nameの後ろのiは大文字小文字を区別しない。

実行結果は

OK

<?php

  $a = "Neverland Of Promise";
  $b = "neverland";

  if (preg_match("/\b$b\b/i", $a)) {
    echo $a . "\n";
  } else {
    echo "NG" . "\n";
  }
?>

\bで囲むとその単語のみマッチする
つまりこの例だとneverだけの場合反応するが、neverlandは反応しない

実行結果

Neverland Of Promise

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0