Regular expression for words between 3 and 16 characters in PHP

I am completely new to regular expressions and I need to filter out all words with at least 3 characters (and a maximum size of 16) from the text. (so I can enter this data into the MySQL database)

Everything currently works, except for the regular expression:

/^.{3,16}$/

(I built it from a tutorial found using Google ;-))

Thank! Ivan

Sample data:

rjm1986 * SinuhePalma * excel2010 * Jimineedles * 209663603 * C6A7XR * Snojog * XmafiaX * Cival2 * HitmanPirrie * MAX * 4163016 * Dredd23 * Daddy420 * mattpauley * Mykillurdeath * 244833585 * KCKnight * Greystoke * Fatbastard * Fucku4 * Davkar * Banchy2 * ET187 * Slayr69 * Nik1236 * SeriousAl * 315791 * 216996334 * K1ra * Koops1 * LastFallout * zmileben * bismark * Krlssi * FuckOff1 * 1owni * Ulme * Rxtvjq * halfdeadman * Jamacola * LBTG1008 * toypark * Magicman6497 * Tyboe187 * Bob187 * Zetrox

PHP Code (yes, I know - this is careless - this is only used to generate requests ...)

<?php
    //regexer.php

    $text = @$_REQUEST['fText'];
    if ($text == '') {
?>
<form method="post" action="">
    <input type="text" name="regex" />
    <textarea name="fText"></textarea>
    <br />
    <input type="submit"></input>
</form>
<?php 
    } else {
        preg_match_all($_REQUEST['regex'], $_REQUEST['fText'], $matches);
        header ("Content-type: text/plain");
        foreach ($matches as $match) {
            //print_r($match);
            echo ("INSERT INTO maf_codes (Code, GameID) VALUES ('$match', %GAMEID%);\n");
        }
    }
?>

Found solution: replace $ _REQUEST ['regex'] with regular expression;)

+3
source share
4 answers

Try the following:

/\b\w{3,16}\b/

Explanations:

  • \b matches the word boundary
  • \w matches the word character
  • {3,16} \w, , 3 16 .

FYI: (^) ($) , , , , ​​ .

UPDATE:

, :

<?php

$input = 'rjm1986 * SinuhePalma * excel2010 * Jimineedles * 209663603 * C6A7XR * Snojog * XmafiaX * Cival2 * HitmanPirrie * MAX * 4163016 * Dredd23 * Daddy420 * mattpauley * Mykillurdeath * 244833585 * KCKnight * Greystoke * Fatbastard * Fucku4 * Davkar * Banchy2 * ET187 * Slayr69 * Nik1236 * SeriousAl * 315791 * 216996334 * K1ra * Koops1 * LastFallout * zmileben * bismark * Krlssi * FuckOff1 * 1owni * Ulme * Rxtvjq * halfdeadman * Jamacola * LBTG1008 * toypark * Magicman6497 * Tyboe187 * Bob187 * Zetrox';

$matches = array();

preg_match_all('/\b\w{3,16}\b/', $input, $matches);

print_r($matches);

?>

:

Array
(
    [0] => Array
        (
            [0] => rjm1986
            [1] => SinuhePalma
            [2] => excel2010
            [3] => Jimineedles
            [4] => 209663603
            [5] => C6A7XR
            [6] => Snojog
            [7] => XmafiaX
            [8] => Cival2
            [9] => HitmanPirrie
            [10] => MAX
            [11] => 4163016
            [12] => Dredd23
            [13] => Daddy420
            [14] => mattpauley
            [15] => Mykillurdeath
            [16] => 244833585
            [17] => KCKnight
            [18] => Greystoke
            [19] => Fatbastard
            [20] => Fucku4
            [21] => Davkar
            [22] => Banchy2
            [23] => ET187
            [24] => Slayr69
            [25] => Nik1236
            [26] => SeriousAl
            [27] => 315791
            [28] => 216996334
            [29] => K1ra
            [30] => Koops1
            [31] => LastFallout
            [32] => zmileben
            [33] => bismark
            [34] => Krlssi
            [35] => FuckOff1
            [36] => 1owni
            [37] => Ulme
            [38] => Rxtvjq
            [39] => halfdeadman
            [40] => Jamacola
            [41] => LBTG1008
            [42] => toypark
            [43] => Magicman6497
            [44] => Tyboe187
            [45] => Bob187
            [46] => Zetrox
        )

)
+5

, ? , , \b:

/\b\w{3,16}\b/

: . :

<?php
$a = array();

preg_match_all('/\b\w{3,16}\b/', "rjm1986 * SinuhePalma * excel2010 * Jimineedles * 209663603 * C6A7XR * Snojog * XmafiaX * Cival2 * HitmanPirrie * MAX * 4163016 * Dredd23 * Daddy420 * mattpauley * Mykillurdeath * 244833585 * KCKnight * Greystoke * Fatbastard * Fucku4 * Davkar * Banchy2 * ET187 * Slayr69 * Nik1236 * SeriousAl * 315791 * 216996334 * K1ra * Koops1 * LastFallout * zmileben * bismark * Krlssi * FuckOff1 * 1owni * Ulme * Rxtvjq * halfdeadman * Jamacola * LBTG1008 * toypark * Magicman6497 * Tyboe187 * Bob187 * Zetrox", $a);

print_r($a);

:

Array
(
    [0] => Array
        (
            [0] => rjm1986
            [1] => SinuhePalma
            [2] => excel2010
            [3] => Jimineedles
            [4] => 209663603
            //.... lot more here...
            [45] => Bob187
            [46] => Zetrox
        )

)

, , :

 foreach ($matches[0] as $match) {
        print_r($match);
        //...
 }

$matches, :

$matches = array();
preg_match_all($_REQUEST['regex'], $_REQUEST['fText'], $matches);
+2

, .

/\b\w{3,16}\b/g

, () , :

/^.{3,16}$/
  • ^ $ . , .
  • . , .
+1

strlen().

$mystr="rjm1986 * SinuhePalma * excel2010 * Jimineedles * 209663603 * C6A7XR * Snojog * XmafiaX * Cival2 * HitmanPirrie * MAX * 4163016 * Dredd23 * Daddy420 * mattpauley * Mykillurdeath * 244833585 * KCKnight * Greystoke * Fatbastard * Fucku4 * Davkar * Banchy2 * ET187 * Slayr69 * Nik1236 * SeriousAl * 315791 * 216996334 * K1ra * Koops1 * LastFallout * zmileben * bismark * Krlssi * FuckOff1 * 1owni * Ulme * Rxtvjq * halfdeadman * Jamacola * LBTG1008 * toypark * Magicman6497 * Tyboe187 * Bob187 * Zetrox";
$s = explode(" ",$mystr);
foreach($s as $v){
    $len=strlen($v);
    if($len>=3 && $len<=16){
        echo "found: $v\n";
    }
}
0

Source: https://habr.com/ru/post/1735602/


All Articles