Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only [a-z][A-Z][0-9] in string using PHP

Tags:

regex

php

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

like image 651
zahir hussain Avatar asked May 24 '10 11:05

zahir hussain


People also ask

What does regex 0 9 mean?

Definition and Usage The [0-9] expression is used to find any character between the brackets. The digits inside the brackets can be any numbers or span of numbers from 0 to 9.

What is the use of Preg_match in PHP?

PHP | preg_match() Function. This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search.

What does AZ mean in regex?

Character classes The character class is the most basic regex concept after a literal match. It makes one small sequence of characters match a larger set of characters. For example, [A-Z] could stand for any uppercase letter in the English alphabet, and \d could mean any digit.

What does preg_ match return?

Definition and Usage The preg_match() function returns whether a match was found in a string.


1 Answers

You can filter it like:

$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text);

As for some symbols, you should be more specific

like image 155
Sarfraz Avatar answered Nov 15 '22 19:11

Sarfraz