簡體   English   中英

如何查找字符串在php中的正則表達式中是否包含兩個點

[英]how to find if a string contains two dot in regular expression in php

我想知道字符串是否包含兩個. 例如,如果字符串包含www.example.com ,則返回true,因為它具有2 . ,否則小於1 . 返回false。

function containsTwoDot($array,$keyword){
   $matches = array_filter($array, function($var) use ($keyword) { return preg_match("/\b".$keyword."\b/i".$keyword, $var); });
   if($matches)
        return true;
   return FALSE;
}

$true[0]='www.sll.dl';
$false[0]='ldfjls.dlfjdflldl';

你可以使用php函數substr_count() -

<?php
$text = 'www.example.com';
echo substr_count($text, '.'); // 2
?>

當然它不會返回真或假。你可以管理。

使用preg_match和轉義點導致它的特殊字符:

$string = 'www.example.com';

preg_match('/\..*\./',$string);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM