Asked  7 Months ago    Answers:  5   Viewed   30 times

How can I get a string that only contains a to z, A to Z, 0 to 9 and some symbols?

 Answers

11

You can test your string (let $str) using preg_match:

if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
    // string only contain the a to z , A to Z, 0 to 9
}

If you need more symbols you can add them before ]

Wednesday, March 31, 2021
 
Gordnfreeman
answered 7 Months ago
15

This will work only for non-nested parentheses:

    $regex = <<<HERE
    /  "  ( (?:[^"\\]++|\\.)*+ ) "
     | '  ( (?:[^'\\]++|\\.)*+ ) '
     | ( ( [^)]*                  ) )
     | [s,]+
    /x
    HERE;

    $tags = preg_split($regex, $str, -1,
                         PREG_SPLIT_NO_EMPTY
                       | PREG_SPLIT_DELIM_CAPTURE);

The ++ and *+ will consume as much as they can and give nothing back for backtracking. This technique is described in perlre(1) as the most efficient way to do this kind of matching.

Wednesday, March 31, 2021
 
KingCrunch
answered 7 Months ago
54

This:

preg_replace("[a-zA-Z0-9_-]", "", $var);

wouldn't even replace those characters, except if the input string is exactly the pattern. By using [] as delimiters, they have not the same effect as their would in the expression itself. You could change your delimiter (e.g.: /), or add some more brackets in the pattern:

preg_replace("/[a-zA-Z0-9_-]/", "", $var);    // this works
preg_replace("[[a-zA-Z0-9_-]]", "", $var);    // this too

Now, to negate a pattern in [], you use ^ at the beginning:

preg_replace("/[^a-zA-Z0-9_-]/", "", $var);

You could also have used the insensitive modifier i to match both lowercase (a-z) and uppercase (A-Z):

preg_replace("/[^a-z0-9_-]/i", "", $var);   // same as above
Wednesday, March 31, 2021
 
drowneath
answered 7 Months ago
52

The standard disclaimer applies: Parsing HTML with regular expressions is not ideal. Success depends on the well-formedness of the input on a character-by-character level. If you cannot guarantee this, the regex will fail to do the Right Thing at some point.

Having said that:

<ab[^>]*>(.*?)</a>   // match group one will contain the link text
Saturday, May 29, 2021
 
lewiguez
answered 5 Months ago
92

Yes, the regex replace within your question update is SAFE. But be aware of that ANY include is dangerous and if you will allow the user to include some unsafe script.

Saturday, May 29, 2021
 
pwaring
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :