Friends,
I'm rushing towards deadline and I think that makes me do childish mistakes.
Here I have validation that requires regex and each time I input valid expression preg_match returns false. It is long now I'm trying to spot error but I cannot! I have googled and AFAICS, things seems alright please help me spot the error.
Thanks,
Stefano
<?php
$string = "37961/T.08";//valid ID, it is supposed to match
$regex = '/^[0-9]{5,}/[a-zA-Z]\.[0-9]{2,}/';
if (preg_match($regex, $string)) {
echo 'matched expression!';
} else {
echo 'unmatched expression pattern';//comes here instead of valid regex!
}
?>
Looks like you need to escape your /
$regex = '/^[0-9]{5,}\/[a-zA-Z]\.[0-9]{2,}/';
Related
I have a string which i want to check with a regex. It is not allowed for it to start with a 0. So please see the following examples:
012344 = invalid
3435545645 = valid
021 = invalid
344545 = valid
etc.
How does this regex look in PHP?
PS. This must be a regex solution!
The REGEX should looks like that :
^[1-9][0-9]*$
PHP Code :
<?php
$regex = "#^[1-9][0-9]*$#";
function test($value, $regex)
{
$text = "invalid";
if(preg_match($regex, $value)) $text = "valid";
return $value+" = "+$text+"\n\r";
}
echo test('012345', $regex);
echo test('12345', $regex);
?>
Well it would be a simple /[1-9][0-9]*/.
Please research your question better next time.
This could have also helped you: Regular expression tester
Edit:
Yeah, the answer got downvoted, because it's missing the anchors and seems to be wrong. For completess' sake, I posted the php code I would use with this regex. And no it's not wrong. It may not be the most elegant way, but I like checking whether the regex matched the whole string afterwards more. One reason is that to debug a regex and see what it actually matched I just have to comment out === $value after return $matches[0]
<?php
function matches($value) {
preg_match("/[1-9][0-9]*/", $value, $matches);
return $matches[0] === $value;
}
//Usage:
if (matches("1234")) {
//...
}
?>
I know this question has been asked many times on SO, but I can't seem to get this to work.
I'm trying to use regular expressions to find matches for any Facebook URL, but not when the URL contains "plugins/like" (as in "http://www.facebook.com/plugins/like")
I've come out with the following, and I'm not quite sure why it is not working:
https?://(www)?\.facebook\.[a-zA-Z]{2,4}/((?!plugins/like).)
Am I making a very obvious mistake? Sorry if it doesn't make sense at all, but I've only trying a hand at PHP for the past five months.
Thank you very much for your time and your help.
Edit:
I'm getting matches for any FB URL so far, but it isn't excluding anything that contains plugins/like
$urls = array(
'http://www.facebook.com/',
'https://facebook.com/plugins/foo',
'http://facebook.ru/plugins/like',
);
$pattern = '#^https?://(www\.)?facebook\.[a-z]{2,4}/(?!plugins/like)#';
foreach ($urls as $url) {
echo preg_match($pattern, $url) . PHP_EOL;
}
Okay?
here is a solution using strpos()
$url = 'http://www.facebook.com/test/plugins/like?somestuff';
$matches = array();
if(preg_match('#^https?://(?:www)?\.facebook\.[a-zA-Z]{2,4}/(.*)$#', $url, $matches)
&& strpos($matches[1], "plugins/like") === false) {
// ok
} else {
// nope
}
You forgot to esacpe special characters in the pattern. Characters like : and / must be following a backslash.
https?\:\/\/(www)?\.facebook\.[a-zA-Z]{2,4}\/((?!plugins\/like).)
All is ok with your re (and it works; I've checked). The only thing I would change is write
(?!.*plugins/like)
instead of (?!plugins/like).. That is for cases when plugins goes not direct after the facebook.com/.
I would like to check if a URL (or any string) contains any form of the following pattern ####-##-##
Does anyone have a handy str_replace() or regular expression to do that?
something like:
contains_date_string($string);
returns true if $string contains ####-##-##
Thank you!!!
if (preg_match('/\b\d{4}-\d{2}-\d{2}\b/', $str)) {
// ...
}
If the word boundary (\b) doesn't do the trick, you could try negative lookbehind and lookaheads:
if (preg_match('/(?<!\d)\d{4}-\d{2}-\d{2}(?!\d)/', $str)) {
// ...
}
As an additional validation, you could use checkdate() to weed out invalid dates such as 9999-02-31 as mentioned in this answer.
Use preg_match in conjunction with checkdate:
function contains_date($str)
{
if (preg_match('/\b(\d{4})-(\d{2})-(\d{2})\b/', $str, $matches))
{
if (checkdate($matches[2], $matches[3], $matches[1]))
{
return true;
}
}
return false;
}
'/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'
'Makes sure things like 1998-13-32 won't get past and validate.'
I got this from google... http://www.devnetwork.net/viewtopic.php?f=29&t=13795
Looks promising. Hope this will help someone on the search for the same as stackoverflow is the most accessible SEO wise.
The test given here should work:
if (preg_match('#[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])#', $str)) {
// do something
}
Whatever regex you choose, be careful! When a machine sees 2004-04-01, it won't be able to distinguish between January 4th and April Fools day unless you tell it otherwise...
i have the following pattern that i am trying to solve with preg_match
http://www.1.com/images/001/001/001/1.jpg
http://www.2.com/images/002/002/002/2.jpg
http://www.3.com/images/003/003/003/3.jpg
http://www.4.com/images/004/004/004/4.jpg
http://www.5.com/images/005/005/005/5.jpg
etc.
i need to get only everything that goes after IMAGES and ends before last slash, for example 002/002/002
Hope i could explain well.
Thank you.
You have to use preg_match_all in this case.
<?php
$in = 'http://www.1.com/images/001/001/001/1.jpg
http://www.2.com/images/002/002/002/2.jpg
http://www.3.com/images/003/003/003/3.jpg
http://www.4.com/images/004/004/004/4.jpg
http://www.5.com/images/005/005/005/5.jpg';
if (preg_match_all('~images/(.*?)\.jpg~i', $in, $matches)) {
print_r ($matches[1]);
} else {
echo 'NOT FOUND';
}
The code below gives me this mysterious error, and i cannot fathom it. I am new to regular expressions and so am consequently stumped. The regular expression should be validating any international phone number.
Any help would be much appreciated.
function validate_phone($phone)
{
$phoneregexp ="^(\+[1-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*$";
$phonevalid = 0;
if (ereg($phoneregexp, $phone))
{
$phonevalid = 1;
}else{
$phonevalid = 0;
}
}
Hmm well the code you pasted isn't quite valid, I fixed it up by adding the missing quotes, missing delimiters, and changed preg to preg_match. I didn't get the warning.
Edit: after seeing the other comment, you meant "ereg" not "preg"... that gives the warning. Try using preg_match() instead ;)
<?php
function validate_phone($phone) {
$phoneregexp ='/^(\+[1-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*$/';
$phonevalid = 0;
if (preg_match($phoneregexp, $phone)) {
$phonevalid = 1;
} else {
$phonevalid = 0;
}
}
validate_phone("123456");
?>
If this is PHP, then the regex must be enclosed in quotes. Furthermore, what's preg? Did you mean preg_match?
Another thing. PHP knows boolean values. The canonical solution would rather look like this:
return preg_match($regex, $phone) !== 0;
EDIT: Or, using ereg:
return ereg($regex, $phone) !== FALSE;
(Here, the explicit test against FALSE isn't strictly necessary but since ereg returns a number upon success I feel safer coercing the value into a bool).
It's the [0-9\\- ] part of your RE - it's not escaping the "-" properly. Change it to [0-9 -] and you should be OK (a "-" at the last position in a character class is treated as literal, not part of a range specification).
Just to provide some reference material please read
Regular Expressions (Perl-Compatible)
preg_match()
or if you'd like to stick with the POSIX regexp:
Regular Expression (POSIX Extended)
ereg()
The correct sample code has already been given above.