Regular Expression Help for Date Validation - mm-dd-yyyy - PHP [closed] - php

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 9 years ago.
Improve this question
if(preg_match('/^[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}$/', '10-30-2013')){
echo 'true';
}
else {
echo 'false';
}
This not give me true. I think I'm wrong with regex. please tell how to correct this regex

I suggest not using regex at all for this -- date validation with regex is a surprisingly difficult thing to get right, due to the variations possible in the number of days in any given month.
Far better to simply use PHP's DateTime class to actually parse it into a date object. This will also validate it; if it doesn't meet the specified format, then it won't parse.
$dateObj = DateTime::CreateFromFormat('m-d-Y',$inputString);
See PHP manual page for CreateFromFormat().

You should use dedicate functions to parse date ie.:
if (strptime ('10-30-2013' , 'm-d-Y') !== false) {
echo 'true'.PHP_EOL;
} else {
echo 'true'.PHP_EOL;
}

Actually as the others confirmed, your regex works fine and returns true.
But as you made your code shrunken, I think the input string you're trying to show us isn't exactly just a date and it's within a string or maybe has trailing spaces!
So using ^ and $ at your regex will result in failure.

Related

Decimal number regular expression, no any . {dot or point } Using PHP [closed]

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 7 years ago.
Improve this question
For example if I had:
1.2
1.65
5
9.5
125
Valid numbers would be 5 and 125.
Invalid numbers : 1.2, 1.65, 9.5
I am stuck in checking whether a number has a decimal or not.
I tried is_numeric but it accepted numbers with a decimal.
A possibility is to try using strpos:
if (strpos($a,'.') === false) { // or comma can be included as well
echo 'Valid';
}
or try it using regex:
if (preg_match('/^\d+$/',$a))
echo 'Valid';
Examples taken from here.
If you are sure the number variable is not a string you can use is_int() to check whether it is valid or is_float() to check whether it is invalid.
But if you handle forms for example the variable is often a string which makes it harder and this is an easy solution which works on strings, integers and floats:
if (is_numeric($number)) {
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer!!
}
}
It's also faster than regex and string methods.
Use is_float() function to find invalid numbers

How to check a string contain alphabet or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a simple php program which is getting the data from the url, but I want to check whether it contains a alphabet or not.
The data which I get from the url is as follows ?query=seller,12. I used ctype_alpha() for this, but I am getting no result for this. I have a rough idea that it can be done by preg_match() but I don't how to do it.
Please help me as I am beginner to php.
Thanks in advance
Here you go,
<?php
$subject = "?query=seller,12";
if(preg_match('/[a-zA-Z]/', $subject)){
echo "It has a alphabet";
}
?>
if you want to print all the characters from that string, you can use like this
preg_match_all('/[a-zA-Z]/', $subject,$matches);
print_r($matches);
$matches is an array of all available matches of the specified pattern
Try this dude.
if (!preg_match('/[^A-Za-z]/', $string)) // '/[^a-z\d]/i' should also work.
{
// string contains only english letters
}

PHP Check if output from MySql is a Digit or String [closed]

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
In php, how do I check if a variable is a string or digit?
I'm doing a count() using mysql, and wanted to know if the output is a string or a digit.
I know about is_numeric() and other functions in php, but is there something, if supplied a value will echo if the value is a digit or string?
Just try with gettype function.
gettype('foo'); // string
gettype(1.23); // double
gettype(155); // integer
The problem is if you use a framework or a data abstraction layer, and this treat the output of the database, customarily data type is lost along the way and everything becomes string.
is_numeric is the most indicated in this cases!!!
Short answer:
Use:
echo $gettype($YourValueFromDB);
and in your situation it will output:
string
OR
if(is_int($YourValueFromDB))
{
echo 'we have an integer type!';
}
elseif(is_string($YourValueFromDB))
{
echo 'we have a string type!';
}
else
{
echo 'Not sure: '.gettype($YourValueFromDB);
}
Long answer:
MySQL produces a number value with this query:
select count(*) as total from table
// can range from 0 - infinity (or exhausted memory limit, whichever comes first)
// for this post, pretend this count() returns a 9
When this data is passed back to PHP it is automatically converted into a string due to PHP's extremely relaxed type-casting.
Running this:
var_dump($row);
would produce something like this:
array
'total' => string '9' (length=1)
indicating that you are working with a literal string but it's value is a number so things like if($row['total'] > 1){ } would go inside the curly braces without a problem.

Convert Range of Numbers to Square Feet [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am using this function which works fine for a single number entered by user in format 1000 and 1,100
function metersToSquareFeet($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*10.7639;
$valFeet = (int)$valInFeet;
if($echo == true)
{
echo $valFeet;
} else {
return $valFeet;
}
}
I need to modify for the case where users enter a range of numbers in the format: 1000-2000
The function should be smart enough to convert both numbers and in this case output in this format 10,763-21,527
You're going to need to parse the input yourself.
If you enter in 1000-2000 as a parameter it will think that it's -1000 as it will do the math.
I'm suggesting you change your input to either 2 parameters, an array or a string.
If using a string, use substring to find the int position of '-' http://us1.php.net/manual/en/function.substr.php or use explode like #crayonviolet suggested
Convert both numbers by your constant (10.7639)
Return your desired output as a string or as part of an array
Your choice depends on how your convertToSquareFeet function is used in other parts of your application and the code rework it would have by changing the parameter list.
Hope that helps!

Getting a value from function [closed]

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'm finishing a script that converts a string and pass it as a link, but besides that, it also shortens the URL with an API. The problem is that I can not think how to get only the URL instead of the entire chain.
The function:
function findAndShort($string) {
$text = preg_replace("/(https?|ftps?|mailto):\/\/([-\w\p{L}\.]+)+(:\d+)?(\/([\w\p{L}\/_\.#]*(\?\S+)?)?)?/u", '$0', $string);
return $text;
}
Example:
$chk = findAndShort("Blahblah http://domain.tld");
echo $chk;
In this case, only need the http://domain.tld, i try with $chk[0], but ofcourse, print the first character on the line..
Just add ^.* at the begining of your regex and use $1 instead of $0:
$text = preg_replace("/^.*((https?|ftps?|mailto):\/\/([-\w\p{L}\.]+)+(:\d+)?(\/([\w\p{L}\/_\.#,]*(\?\S+)?)?)?)/u", '$1', $string);
You can also simplified a bit,
~^.*((https?|ftps?|mailto)://[-\p{L}\p{N}_.]+(:\d+)?(/([\p{L}\p{N}/_.#,]*(\?\S+)?)?)?)~u
The thing with functions is you can only return a single value that is under the return in the function, so in your case, after testing your function, the return value is the original input value.
If you want a function to return only the URL component I would use explode to separate the string then run preg_match on each part of the resulting array from the explode function looking for the https, ftp or mailto, once I know what part of the array I'm working with I would stop the process, define the var and as I believe you want to return code for a link, I would do is then do something like
return ''.$url.'';
Note that the var $url would be created from the above process.
I hope this helps.

Categories