Jump to content
php.lv forumi

preg_match priekš trīs vienādiem simboliem pēc kārtas


mashiina

Recommended Posts

Sveiki.

 

Vai varētu kāds palīdzēt vai padalīties ar preg_match`u:

1) qwerty- valīds;

2) qwerrrty- nav valīds;

 

Respektīvi, ja trīs vienādi simboli pēc kārtas, tad nav valīds.

Edited by mashiina
Link to comment
Share on other sites

<?php

// Rinda kas var vai nevar saturet qwerty
$string = "qwerty";


// Satur rindu
if (preg_match("/\bqwerty\b/i", $string)) {
echo "valid.";
} else {
echo "not valid.";
}




// Rinda kas var vai nevar saturet qwerty
$string = "qwerrrrty";

// Nesatur rindu
if (preg_match("/\bqwerty\b/i", $string)) {
echo "valid";
} else {
echo "not valid";
}
?>

 

Ja gribi, lai pārbauda vai atkārtojas kāds konkrēts simbols, tad nomaini:

preg_match("/\bqwerty\b/i", $string)

 

uz

 

preg_match("/qwe(r){1,3}ty/",$string);

Edited by WHOAMi
Link to comment
Share on other sites

WHOAMi, qwerty bija domāts tikai kā paraugs, man liekas, vajadzētu spēt validēt jebkuru vārdu...

 

Es, godīgi sakot, uz sitiena nevaru iztēloties reg izteiksmi, kas to spētu :)

 

jā, es sapratu, bet cik es skatījos un pat paguglēju, kaut ko tik specifisku var izdarīt uzkodējot atsevišķu moduli, kurš filtrēs katru vārdu, tāpēc pievienoju potenciālo variantu.

Edited by WHOAMi
Link to comment
Share on other sites

Mierīgi var bez regexpa.

 

Pametīšu ideju:

 

function rep($str){
$len = strlen($str);
for($i=0;$i<$len-3;$i++){
  $c1 = substr($str, $i, 1);
  $c2 = substr($str, $i+1, 1);
  $c3 = substr($str, $i+2, 1);
  if($c1 === $c2 && $c2 === $c3){
 	return false;
  }
}
return true;
}

Link to comment
Share on other sites

Gandrīz strādā:

<?php

$words = array(
   'lol',
   'lool',
   'loool',
   'looool',
);

foreach ($words as $w)
{
   $matches = array();
   preg_match("/(.)\g{1}+/i", $w, $matches);

   foreach ($matches as $m)
   {
       if (strlen($m) == 3)
       {
           echo 'invalid';
           break;
       }
   }
   echo ' (', $w, ')<br>';
}

Edited by Kavacky
Link to comment
Share on other sites

Nu, kur problēma?

 

`(?:(.)\1{2,})`

 

Briedi, pamācies regexus — un tikai tad saki, ka nevar X vai Y ar tiem izdarīt. #burn

 

P.S. Vai varbūt tev vajag, lai mečo tikai UTF-8 burtus? Arī var.

Link to comment
Share on other sites

There, I fixed it:

 

+    	'lāāĀl',

- preg_match("/(.)(\g{1}+)/i", $w, $matches);
+ preg_match("/(.)(\g{1}+)/u", $w, $matches);

- 				if (strlen($m) == 3)
+ 				if (mb_strlen($m, 'UTF-8') == 3)

 

<?php

$words = array(
   	'lāl',
   'lāāl',
   	'lāāāl',
   	'lāāĀl',
   	'lāāāāl',
);

foreach ($words as $w)
{
   	$matches = array();
   	preg_match("/(.)(\g{1}+)/u", $w, $matches);

   	foreach ($matches as $m)
   	{
           	if (mb_strlen($m, 'UTF-8') == 3)
           	{
                   	echo 'invalid';
                   	break;
           	}
   	}
   echo ' (', $w, ')<br>';
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...