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';
}
Related
I have some words with | between each one and I have tried to use preg_match to detect if it's containing target word or not.
I have used this:
<?php
$c_words = 'od|lom|pod|dyk';
$my_word = 'od'; // only od not pod or other word
if (preg_match('/$my_word/', $c_words))
{
echo 'ok';
}
?>
But it doesn't work correctly.
Please help.
No need for regular expressions. The functions explode($delimiter, $str); and in_array($needle, $haystack); will do everything for you.
// splits words into an array
$array = explode('|', $c_words);
// check if "$my_word" exists in the array.
if(in_array($my_word, $array)) {
// YEP
} else {
// NOPE
}
Apart from that, your regular expression would match other words containing the same sequence too.
preg_match('/my/', 'myword|anotherword'); // true
preg_match('/another/', 'myword|anotherword'); // true
That's exactly why you shouldn't use regular expressions in this case.
You can't pass a variable into a string with single quotes, you need to use either
preg_match("/$my_word/", $c_words);
Or – and I find that cleaner :
preg_match('/' .$my_word. '/', $c_words);
But for something as simple as that I don't even know if I'd use a Regex, a simple if (strpos($c_words, $my_word) !== 0) should be enough.
You are using preg_match() the wrong way. Since you're using | as a delimiter you can try this:
if (preg_match('/'.$all_words.'/', $my_word, $c_words))
{
echo 'ok';
}
Read the documentation for preg_match().
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")) {
//...
}
?>
Please I have I want to use regex to preg_match this kind of string :
$liste = 'bla0bla-__my_separator_-01blabla';
I've tried :
if(preg_match('/^[a-zA-Z0-9]_my_separator_[a-zA-Z0-9]$/', $liste))
echo 'ok';
else echo 'not ok';
But this returns always not ok.
Please masters any advise ?
PS : I think that the problem is the _ and the - that what I've tried does not support !
Thanks in advance.
Change your regex pattern to this:
'/^[a-zA-Z0-9_\-]+?_my_separator_[a-zA-Z0-9_\-]+?$/'
If you're only trying to determine if a static substring exists in a string you should use strpos():
if (strpos('_my_separator_', $liste) !== false) {
echo 'ok';
}
You're missing the repeater + and - and _ in your whitelists:
/^[a-zA-Z0-9\-_]+_my_separator_[a-zA-Z0-9\-_]+$/
Your orignal regex would match things like:
A_my_seperator_B
0_my_seperator_C
but not:
AB_my_seperator_C
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...
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,}/';