PHP regular expression with a specific word _my_separator_ - php

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

Related

preg_match php with brackets and determined string is not working

Hi why is the preg_match with brackets and determined string is not working?
Pls let me know your solution to search for "uRole('Admin')".
<?php
$check ='if(uRole("Admin")||uRole("Design")){';
preg_match('/uRole("Admin")/i', $check)? echo 'match':"";
?>
Two reasons this isn't working...
You have to escape () in REGEX (otherwise it's assuming a group)
You don't use echo in a ternary operator; it has to come before
echo (STATEMENT_TO_EVALUATE = [TRUE | FALSE]) : TRUE_VALUE : FALSE_VALUE;
Working code
$check ='if(uRole("Admin")||uRole("Design")){';
echo preg_match('/uRole\("Admin"\)/i', $check, $matches) ? "match" : "";
That is because the ( and ) is a special character in regular expressions used to create groups.
Because it is a special character, you should use a backslash to escape it:
if (preg_match('/uRole\("Admin"\)/i', $check)) {
echo 'match';
}
By the way, in this situation a simple stripos is probably more appropriate:
if (stripos($check, 'uRole("Admin")') !== false) {
echo 'match';
}

String cannot start with zero

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")) {
//...
}
?>

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

preg_match and PHP concatenating

Sorry to bother you but I'm having some trouble with my preg_match parameters. This isn't so much a preg_match question as it is a concatenating question. If I have a variable like $a['state'] and I'm trying to match it to an array of possible states, how do I handle the slashes? This is what I have now but it doesn't work:
if (preg_match("/'" . $a['mystate'] . "'/",$row['state'])){
echo 'yes, a match';
}else{
echo 'got nothing';
exit;
}
Try doing an in_array call instead:
echo (in_array($a['mystate'], $row['state'])) ? 'Match found!' : ' No match';
It sounds to me that you don't need preg_match but strpos instead:
http://php.net/manual/en/function.strpos.php
if ( strpos($a['mystate'],$row['state']) !== FALSE ){
// match
}else{
// no match
}
OR, event better solution would be to use in_array...
EDIT:
I saw your comment and `in_array' is definetely the function you're looking for...
http://php.net/manual/en/function.in-array.php

php Preg_match pattern to solve

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';
}

Categories