Help us understand the problem. What is going on with this article?

Preg_match関数

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

<?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

tabiojisann
WEB、システム開発に携わりたくて、現在求職中です。 現在は自動化技術(DevOps)とビッグデータ解析に興味あります。
https://www.wantedly.com/users/130056727
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした