mashiina Posted June 4, 2012 Report Posted June 4, 2012 (edited) 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 June 4, 2012 by mashiina Quote
WHOAMi Posted June 4, 2012 Report Posted June 4, 2012 (edited) <?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 June 4, 2012 by WHOAMi Quote
briedis Posted June 4, 2012 Report Posted June 4, 2012 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 :) Quote
WHOAMi Posted June 4, 2012 Report Posted June 4, 2012 (edited) 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 June 4, 2012 by WHOAMi Quote
briedis Posted June 5, 2012 Report Posted June 5, 2012 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; } Quote
Kavacky Posted June 5, 2012 Report Posted June 5, 2012 (edited) 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 June 5, 2012 by Kavacky Quote
daGrevis Posted June 5, 2012 Report Posted June 5, 2012 Nav iespējams... nu ja. `(?:([a-z0-9])\1{2,})` Quote
daGrevis Posted June 5, 2012 Report Posted June 5, 2012 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. Quote
briedis Posted June 5, 2012 Report Posted June 5, 2012 Briedi, pamācies regexus — un tikai tad saki, ka nevar X vai Y ar tiem izdarīt. #burn burn šmurn, es nevienā brīdī neteicu, ka tas nav izdarās, bet teicu, ka uz sitiena nevaru izdomāt :) Quote
Kavacky Posted June 5, 2012 Report Posted June 5, 2012 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>'; } Quote
rpr Posted June 7, 2012 Report Posted June 7, 2012 risinājums ir asprātīgs, bet ko īsti nozīmē "?:" Quote
daGrevis Posted June 7, 2012 Report Posted June 7, 2012 http://www.regular-expressions.info/refadv.html Quote
rpr Posted June 8, 2012 Report Posted June 8, 2012 manupraat var iztikt bez taam aareejaam iekavaam un tad nevajag arii to ?: un viss izskataas daudz saprotamaak. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.