Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
when user submit a form input text
i want it just to allow a-z 0-9 and -_ just this numbers digits and -_
how do i check the input and make it only this inputs allows and delete the rest
Thank You!
$output = preg_replace("/[^a-zA-Z0-9_\-]/", '', $input);
function validate($value){
return preg_match("/^[a-zA-Z0-9_\-]*$/", $value) !== 0;
}
If the return value is false then you have invalid characters, if it true, everything is correct.
$input = preg_replace("/[^0-9a-z\_\-]/i", "", $_POST["myfield"]);
Use regexp.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
What can be the regex to validate the string as below
0000-XXXX-XXXX
four digits at start then hyphen then 4 alphabets then hyphen and then 4 alphabets again.
Thank you.
$string="1234-ABCD-EFGH";
preg_match("~[0-9]{4}-[A-Z]{4}-[A-Z]{4}~", $string, $matches);
print_r($matches);
This code will output the $matches array that contains the match information.
preg_match will return true or false depending on whether the string matched.
If you prefer to also match lower case characters, use this one:
preg_match("~[0-9]{4}-[A-Za-z]{4}-[A-Za-z]{4}~", $string, $matches);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am currently doing this:
$a = 'some string with special characters';
$a = htmlentities($a);
$a = trim(preg_replace('/&#?[a-z0-9]+;/i', '', $a));
This works but I am wondering if there's a more efficient way to do this?
it contains certain characters like  and I want to remove those.
Try utf8_decode, or more feature-filled:
$output = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $input);
Documentation
The reason I bring this up is because what you are seeing are encoding issues, not "special characters".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to validate for UK phone numbers - the specific requirements I have are:
must only consist of digits
the only non-digit characters allowed are space and dash
must begin with a zero
Can anyone suggest a nice and simple regular expression I can use for this?
Try with following regex:
^0([ -]\d+)*\d+$
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Is there any way that I can get o identify characters like(trademark, superscripts, numbers) from text or text file(but these characters are't in HTML code) and replace them using php
Example:
Get from text: ™ replace with PHP: (TM)
Yes, just parse your strings with str_replace.
Note that str_replace can use arrays, so you can replace multiple strings at once:
$text = "Text™®";
$text = str_replace(['™', '®'], ['(TM)', '(R)'], $text);
print $text;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Not terribly familiar with regex, but I'm guessing it's going to make life much easier for my current need.
I need to validate that a string contains the correct sequence of numbers and letters, so that it always follows the format "AA99999A", where A can be any A-Z character and 9 can be any 0-9 character. Other than exploding the string and validating the characters individually, how would the best way to handle this be?
The result can be a simple true / false as I don't need to specify which characters are incorrect
How about:
preg_match('/^[A-Z]{2}\d{5}[A-Z]$/', $string);
You can try this regex:
'/^[A-Z]{2}\d{5}[A-Z]$/i'
PHP's preg_match will do the trick:
$string_to_be_validated = "AB12345C";
if (preg_match("/\A[A-Z]{2}[0-9]{5}[A-Z]{1}\z/", $string_to_be_validated)) echo "valid";
else echo "invalid";
Here you can find more information on this.