Regular Expression for 2 or more strings, with php - php

I have strings like this : "ABC/ABC/123", "ABC/ABC/123/567", "ABC/123"
And I want to search the strings that have 2 ore more "/"
How can I do that with regular expression ?
EDIT
I have found a solution like this:
´if($sku1!=$sku2 && preg_match("#^".$sku1."[/]#", $sku2) && substr_count($sku2, '/')==1) {´
Any improvement?
Thanks for help.

I agree with adrien: you have every reason not to use regex for this, though I'd suggest another solution:
$string = 'ABC/ABC/123';
if (count(explode('/',$string)) > 2)
{
echo $string.' contains at least 2 slashes';
}
EDIT
As the OP pointed out in his comment below: substr_count($string,'/') is the easiest and fastest way to check for 2+ slashes...

Yes you can use the regex:
^([^\/]+\/){2,}$

Without regexp (so much faster) :
$string = "ABC/ABC/123";
if (strpos($string, '/', strpos($string, '/') + 1) !== FALSE) {
echo "2 or more slashes in this string!";
}

Related

check words with preg_match

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().

How to find an occurence in string?

I would like to know what is the best way to know if my substring is in the string.
So, I can has these kind of value :
"abcd.tif"
"abcd_A.tif"
"abcd_B.tif"
"abcd_VP.tif"
I would detect if "_A" or "_B" or "_VP" is present in the string.
What's the best way ? Using a combination of substr and strlen ?
or use regex ?
Use strpos, which will give you the 0-indexed position of the needle in the string. If it's not in the string, strpos returns false. (There is also case-insensitive stripos).
However, you can only check one needle at a time. If you'd like to check for any of the three simultaneously you can either use a loop or write the terser but less efficient way:
preg_match('/_A|_B|_VP/', $str)
...which will return true if any of those three strings matches.
The most efficient way (I know of :P) is strpos.
$s = 'abcd_VP.tif';
if (strpos('_A', $s) !== false) {
// do your thing
}
You can do a simple || after this, it won't be as short, but it will be much quicker than regex:
$s = 'abcd_VP.tif';
if ((strpos('_A', $s) !== false) || (strpos('_B', $s) !== false) || (strpos('_VP') !== false)) {
// do your thing
}
Use OR operation like (_A)|(_VP) etc. check this question as a hint: Regular Expressions: Is there an AND operator? , to use 'OR' in regular expression.

PHP regular expression with a specific word _my_separator_

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

preg_match lazy?

Hey guys I'm trying to strip out the last _1.jpg if it
exists in my expression, but I am having some trouble
Scenarios are like follows.
image_name_1.jpg (should be image_name.jpg)
image_name_1_1.jpg (should be image_name_1.jpg)
I came up with this, but it seems to be returning null
$pic = ($pic[strlen]-6 == '_') ? preg_replace('/*.\_[0-9]/', '.jpg', $pic) : $pic;
What's wrong with my formula? Could someone help?
*. makes here truble. You can use *.? - the ? mean lazy quantification.
But your expression havent to pattern whole input string, only target - see ohaal's answer.
This should do it:
preg_replace('/_\d\.jpg$/', '.jpg', $pic);
Use strrpos along with ohaal's expression:
$pic_name=(strrpos($pic,'_',-6)) ? preg_replace('/_\d\.jpg$/','.jpg',$pic) : $pic;
Link:
http://php.net/manual/en/function.strrpos.php

How can I specify an interval number using regex?

I have to check if a variable (php, preg_match) is from 1988 and 2011 using regex; I know how to do it with normal if/else, but I'd like to use regex for this!
Sometimes, regular expressions are not the only answer:
if (preg_match('/^\d{4}$/', $input) && $input >= 1988 && $input <= 2011) {
}
Wouldn't be that easy, as regex is meant to match character by character. You could use something like this (probably wouldn't be a good idea).
/(198[89]|199\d|200\d|201[01])/
Why do you want to do this using regex?
One solution could be something along the lines of (?:198[8-9]|199[0-9]|200[0-9]|201[0-1]).
Use preg_replace_callback :
<?php
preg_replace_callback('%([0-9]{4})%', function($match) {
$number = $match[1];
if($number < 1988 || $number > 2011) return; /* Discard the match */
/* Return the replacement here */
}, $input);
This is in my opinion the most flexible solution.
Try this:
/^[12][90][8901][8901]\z/

Categories