简体   繁体   English

如何查找字符串在php中的正则表达式中是否包含两个点

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

I want to know if the string contains two . 我想知道字符串是否包含两个. . for example if the string contains www.example.com , returns true ,because it has 2 . 例如,如果字符串包含www.example.com ,则返回true,因为它具有2 . , otherwise has less than 1 . ,否则小于1 . return false. 返回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';

You can use php function substr_count() - 你可以使用php函数substr_count() -

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

of course it doesn't return true or false .That you can manage. 当然它不会返回真或假。你可以管理。

Use preg_match and escape dots cause it's special chars: 使用preg_match和转义点导致它的特殊字符:

$string = 'www.example.com';

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在PHP中使用正则表达式从字符串中找到子字符串? - How to find a substring from a string using regular expression in php? PHP使用正则表达式查找字符串的一部分 - Php find a part of string using regular expression PHP正则表达式查找并附加到字符串 - PHP regular expression find and append to string 正则表达式在PHP中查找字符串模式 - Regular expression to find pattern in string in PHP php正则表达式在文件中查找字符串 - php regular expression to find a string in a file 如何匹配包含任意字符串的正则表达式,并使用PHP仅将该任意字符串转换为变量? - How to match a regular expression that contains an arbitrary string, and get only that arbitrary string into a variable using PHP? 如何使用正则表达式和 PHP 从包含背景图像的 CSS 文件中查找所有类名或 ID? - How to find all class names or ids from a CSS file that contains a background image using Regular expression and PHP? PHP正则表达式。 检查String是否仅包含字母 - PHP Regular Expression. Check if String contains ONLY letters PHP /正则表达式检查字符串是否包含一定长度的单词 - PHP / regular expression to check if a string contains a word of a certain length PHP正则表达式-检测字符串是否包含单词但不在另一个单词之内 - PHP Regular Expression - Detect if string contains a word but not within another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM