preg_match() $pattern issues with format [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm looking at the preg_match instructions, but struggling to gather together what I need to only allow a certain few characters to be used, and how to implement that into the pattern format
if(preg_match('[0-9.$M]', $_POST['budget']) === true) {
$errors = 'no illegal';
} else {
//Illegal character in budget
$errors = 'illegal';
}
But I've realized that for one that doesn't work for this combination
0123456789$M. as well as that it will not function as I want it to even if I get it to work. as I need to check the $_POST['budget'] and be sure it's in the correct format before moving on with it. It can ONLY contain the characters I've put forward, anything else will create a pretty big mess.
So far, I've had javascript change any entries, but if that is disabled then they can put in whatever they please.
So I need is pseudo
if (preg_match($allowed_characters, $_POST['budget'] === true && DOESNT CONTAIN ANY OTHER CHARACTERS) {
//No illegal characters
} else {
//Illegal characters!!
}

1) preg_match returns INT or BOOL false.
2) Your pattern is not valid for preg_match
should be something like:
if(preg_match('/[^0-9\.\$M]+/', '0123456789$M.')) {
$errors = 'illegal';
} else {
$errors = 'no illegal';
}

Related

If else not running on Boolean value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Tried the below code. All I can ever get is "not working".
bp_is_my_profile(); //this is a Boolean.
echo bp_is_my_profile(); // this echo's out either one or nothing depending on true or false.
if ($bp_is_my_profile === "true") { // I've tried putting in numbers one and zero.
echo "Have a good morning!";
} elseif ($bp_is_my_profile === "false") { // I've tried without quotes.
echo "Have a good day!";
} else {
echo "Not working"; //whatever the Boolean is, each's it prints this line of code.
}
Call the function in the if
if (bp_is_my_profile()) {
echo "Have a good morning";
} else {
echo "Have a good day";
}
You shouldn't compare with strings, since the function returns a boolean. And since it returns a boolean, there are only two possibilities, so you don't need three cases.

PHP Regex for password: minim 4 charaters, of which at least one CAN'T be a letter or a number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have to make a regex for a password with the following conditions:
- minimum 4 characters
- at least 1 non-word character.
I tried this way:
$regex_password = '#(\W)+#';
$regex_password1 = '#(?i)([a-z\d])+#';
if ((!preg_match($regex_password, trim($_POST['pass']))) && (!preg_match($regex_password1, trim($_POST['pass']))) && strlen(trim($_POST['pass'])) <5){
//error
}
And then tried to create a new account with the password: "pas" and it's working, so there's something wrong with my regex. Can someone help?
You just have to change your && to || i.e. if any of condition false then it will echo an error
if ((!preg_match($regex_password, trim($_POST['pass']))) || (!preg_match($regex_password1, trim($_POST['pass']))) || strlen(trim($_POST['pass'])) <5){
echo "error";
}
else
{
echo "ok";
}

Using strpos to finds words in a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
so I am trying to check if a string contains a specific set of words, and set a variable if they do. I have the code below, first it was working great except that it would not work if one of the key words was the very first word, so I looked at the php manual and found out about === and implemented that, but now it sets the variable to one every time even if none of the words are found!
So basically it reads a text file to an array, the text file contains the key words, then it checks the string to see if any of those key words are present in the sting. If none of the key words are found then wc would equal 0 and so would inc. If it finds any then wc is incremented every time a word is found, and if it is greater than 0 it will set inc to 1 to flag that key words were included.
Hopefully that all makes sense....
Here is my code:
$inc = 0;
$list = file("filter.txt", FILE_IGNORE_NEW_LINES);
$cnt = count($list);
$wc = 0;
for ($i=0; $i<$cnt; $i++)
{
if (strpos($string,$list[$i]) === false)
{
$wc ++;
}
if ($wc > 0)
{
$inc = 1;
}
}
It doesn't work because you are increasing wc if the string is not found.
You have to replace === with !==.

PHP regex not working and keeps giving me the error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm stumbling on a regex issue and not sure why this validation gives me an error after entering in a correct string.
// City validation
if (empty($custom_fields["city_id"])) {
$response["error"] = TRUE;
$response["response"] = "City field is missing. Please try again.";
unset($_POST["s2member_pro_paypal_registration"]["nonce"]);
} elseif (!preg_match('^[a-zA-Z]+(?:[\s-]+[a-zA-Z]+)*$', $custom_fields["city_id"])) {
$response["error"] = TRUE;
$response["response"] = "Invalid City name";
unset($_POST["s2member_pro_paypal_registration"]["nonce"]);
}
I entered New York and checked the regex expression in http://gskinner.com/RegExr/. It works fine but I get an error upon submitting the input string.
Can someone please assist me?
You need to use required regex delimiter in your preg_match code as this one:
preg_match('/^[a-zA-Z]+(?:[\s-]+[a-zA-Z]+)*$/', $custom_fields["city_id"])

PHP phone preg_match [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
For a class I need to do a preg_match for a phone number. The number must match this format (###) ###-####. If the format is incorrect I need to send the user to the form to have the number filled out again. If a match is found I need to allow the form to go through.
My instructor had us write it as such:
$PhonePattern = "\(\d\d\d\) \d\d\d-\d\d\d\d";
if (!preg_match($PhonePattern, $Phone))
{
header('location:CreateAccount.htm');
exit();
}
else
{
$Phone = $_GET['txtPhone'];
}
This does not work. Can it be changed to:
if (!preg_match('/\(\d\d\d\) \d\d\d-\d\d\d\d/', $Phone))
{
header('location:CreateAccount.htm');
exit();
}
else
{
$Phone = $_GET['txtPhone'];
}
The following is one way:
'/^\(\d{3}) \d{3}-\d{4}$/'
I added anchors and repetition. Otherwise, you were close.
Learn more about regular expressions. They are a power tool. But with great power, comes great responsibility.

Categories