php htmlタグが含まれているかどうか判別するサンプル

環境
PHP 8.1.2
Ubuntu 22.04.1 LTS

構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
preg_match(
string $pattern,
string $subject,
array &$matches = null,
int $flags = 0,
int $offset = 0
): int|false
preg_match( string $pattern, string $subject, array &$matches = null, int $flags = 0, int $offset = 0 ): int|false
preg_match(
    string $pattern,
    string $subject,
    array &$matches = null,
    int $flags = 0,
    int $offset = 0
): int|false

pattern
検索するパターンを表す文字列。

subject
入力文字列。

matches
matches を指定した場合、検索結果が代入されます。

操作例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function is_match_tags($str) {
if (preg_match('/<.*>/', $str)) {
return true;
} else {
return false;
}
}
if (preg_match(<p>PHP</p>)) {
// htmlタグが含まれている場合
}
function is_match_tags($str) { if (preg_match('/<.*>/', $str)) { return true; } else { return false; } } if (preg_match(<p>PHP</p>)) { // htmlタグが含まれている場合 }
function is_match_tags($str) {
      if (preg_match('/<.*>/', $str)) {
            return true;
      } else {
            return false;
      }
}

if (preg_match(<p>PHP</p>)) {
      // htmlタグが含まれている場合
}

 

IT

Posted by arkgame