繁体   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