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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to know how to verify that the user entered empty string in a sentence if in a string with multiple spaces in blank
Example
" "
if user entered a emṕty string, the program must show an alert as this
echo "The username must not be empty";
Use trim to remove whitespace from a var...
$name = trim($_GET['name']);
if ($name == '') //empty
Try this
if (strlen(trim($yourString)) == 0) {
// Do something
}
If the length of the string is 0 after the spaces are trimmed/removed.
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';
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm someone of a newbie. I'm not sure what the best way to prevent xss or injections on this field would be.
I have a field on my site that will take a user-submitted IP address or URL in order to complete a ping/traceroute test. I really only want to allow what HAS to be allowed for the thing to be functional. I have the whole thing working right now, but I'm not sure how to sanitize the input.
Basically, here's my function for ping. I don't know a LOT about output buffering, but it seemed like the easiest way to capture the output from the command.
function ping($domain){
echo "</div>";
$command = shell_exec("ping -c 3 $domain");
ob_start();
echo "<pre>$command</pre>";
$output = ob_get_clean();
echo "\n" . $output;
}
You could make use of PHP's built-in filtering with filter_var()
if($domain === filter_var($domain, FILTER_VALIDATE_URL) || $domain === filter_var($domain, FILTER_VALIDATE_IP)
{
// run your existing code
}
else
{
// invalid input
}
I'm sure something like the following should work well:
if(!preg_match('/^[a-z0-9]+\.[a-z0-9]+$/i', $domain) &&
!preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/i', $domain))
die('Not a valid domain!');
Example here: http://phpfiddle.io/fiddle/843401527
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"])
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Suppose we have the following link
$link = "http://www.mysite.com/bbbbbbb.flv?something";
And we want to matching it.
Upon this question how to match links that has extension #Thanks to all#
We have two ways regular expression and parse_url
So my question which is better in your point of view and for my website (such as fast better accurate ..etc)?
Method 1 regular expression
if (preg_match('/\.flv(\?|$)/i', $link)) {
echo 'It has flv';
} else {
echo 'It does not have';
}
Method 2 parse_url
$arr = parse_url($link);
$pathParts = pathinfo($arr['path']);
if ($pathParts['extension'] == 'flv')
echo "valid extension";
else
echo "invalid extension";
# Thanks
According to this publication, using the built in parse_url is much slower than preg_match. This is understandably the case, because the regular expression captures a single result, whereas parse_url captures and parses all components of the url.