2017-05-06 17 views
1

我需要检查,如果查询字符串是小于4个字符的话由纯粹,然后删除所有的空格,如果它是真实的话。PHP - 检查字符串只有不到4个字符

因此,像:this has four character words or higher ...将返回FALSE

喜欢的东西:hd 1 kit ...将返回TRUE为字符串中没有的字超过3个字符。

我想尝试编码,但还没有关于如何写一个正则表达式这样的事情丝毫线索。

回答

3

希望这个简单的解决方案将帮助你。

正则表达式:/\b[a-zA-Z0-9]{4,}\b/

1.\b[a-zA-Z0-9]{4,}\b将匹配将四个字符和\b为boundry条件。

<?php 

$string1="this has four character words or higher"; 
$string2="hd 1 kit"; 

if(!preg_match_all("/\b[a-zA-Z0-9]{4,}\b/", $string1)) 
{ 
    echo "Should be allowed"; 
} 
+0

谢谢,非常完美,我应该想到,为什么不尝试搜索的4个字符和真/假关闭的至少1个字。非常感谢! –

+0

如果您认为只需要*一个*字,并且至少有*四个字符,则可以使其更简单。 –

+0

@CasimiretHippolyte我改变了它,因为在SO上的一个好人给了我一个像这样的字符串'我是Sahil'。这不会有'4'字一个字,但它应该被禁止.. –

3

你可以用正则表达式做到这一点像@SahilGulati提出,但它可能是更有效地使用explode()

$string = "this has four character words or higher"; 
$array = explode(" ", $string); 
$success = true; 
foreach ($array as $word) { 
    if(strlen($word) < 4) { 
     $success = false; 
     break; 
    } 
} 
if($success) { 
    echo "ok"; 
} else { 
    echo "nok"; 
} 

这里是一个live example


而且here是使用正则表达式和非正则表达式(约35%的速度不使用正则表达式时)现场比较:

<?php 
function noRegex() { 
    $string = "this has four character words or higher"; 
    $array = explode(" ", $string); 
    $success = true; 
    foreach ($array as $word) { 
     if(strlen($word) < 4) { 
      $success = false; 
      break; 
     } 
    } 
    return $success; 
} 
function regex() { 
    $string = "this has four character words or higher"; 
    $success = false; 
    if(!preg_match_all("/\b[a-zA-Z0-9]{4}\b/", $string)) { 
     $success = true; 
    } 
    return $success; 
} 

$before = microtime(true); 
for($i=0; $i<2000000; $i++) { 
    noRegex(); 
} 
echo "no regex: "; 
echo $noRegexTime = microtime(true) - $before; 
echo $noRegexTime; 
echo "\n"; 

$before = microtime(true); 
for($i=0; $i<2000000; $i++) { 
    regex(); 
} 
echo "regex: "; 
echo $regexTime = microtime(true) - $before; 
echo $regexTime; 
echo "\n"; 

echo "Not using regex is " . round((($regexTime/$noRegexTime) - 1) * 100, 2) . "% faster than using regex."; 
?> 
+0

我选择我选择,因为我在这里查询已经变得在不到一秒钟处理的低使用环境的答案。我更喜欢更少的代码。我的最终结果是:'$ query =(!preg_match_all(“/ \ b [a-zA-Z0-9] {4,} \ b /”,$ query)?str_replace(“”,“”,$ query): $ query);' –

+0

该解决方案不考虑标点符号。有了正则表达式,你根本不会考虑它们,但是在爆炸的情况下,我想你需要修剪它们。 – sevavietl

+0

你为什么要在你的基准测试中使用'preg_match_all'? ['return(bool)!preg_match(“/ \ b [a-zA-Z0-9] {4} /”,$ string);'](http://sandbox.onlinephpfunctions.com/code/0f733df15dd8c305f716e1daf3cb1f584f0dfeff)和它优于noregex。 –

0

如果你没有在字符串中的标点符号,则最有效的方法将是使用strpos

function checkWordsLenght($string, $limit) 
{ 
    $offset = 0; 
    $string .= ' '; 

    while(($position = strpos($string, ' ', $offset)) !== false) { 
     if (($position - $offset) > $limit) { 
      return false; 
     } 

     $offset = $position + 1; 
    } 

    return true; 
} 

这里是working demo

0

重要的是,提供基于正则表达式的解决方案时,这个答案被认为是“最好的”是最精致的。这意味着提供最准确的结果,并且当结果准确性相关时,性能应该成为下一个标准,如果涉及到这一点,则应遵循模式简洁性。

出于这个原因,我不得不发布一个答案,它优于目前公认的答案。我将在ssc-hrep3的答案下使用V_RocKs在注释中使用的变量名称。

代码使用第一样本串:

$query="this has four character words or higher"; 
$query=preg_match("/[^ ]{4,}/",$query)?str_replace(" ","",$query):$query; 
echo "$query"; 

输出:

$query="hd 1 kit"; 
$query=preg_match("/[^ ]{4,}/",$query)?str_replace(" ","",$query):$query; 
echo "$query"; 

输出::

hd 1 kit 
使用第二样本串

thishasfourcharacterwordsorhigher 

代码

不仅是我正则表达式模式同样准确,它是较短的,更有效的(需要更少的步骤)。对于这个问题,使用边界字符是没有必要的,它对性能的影响将近50%。

从模式掉落字边界后,有几种方法可以针对所需的子字符串。下面的模式有相同的意义和steps计数:

  • /[a-zA-Z0-9]{4,}/
  • /[a-z0-9]{4,}/i
  • /[a-z\d]{4,}/i
  • /[^ ]{4,}/

我的观点是:读者不搜索,这样来“这就够了”的答案,他们来到这里从广阔的知识基础中吸取有启发性的教育方法和多样的SO社区。让我们按下,在每一个答案上达到最好的方法,以便将来的读者可以从我们的见解中学习,并接受所有编码语言必须提供的教育。

当次优模式upvoted /绿色打勾如此,还有一个错失的机会,以适当地教育读者,以完成编码任务的最佳方式。