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
Related
I need to search inside a string to find if there ise any matches to this pattern:
class="%men%"
It means that the class may be equal to either:
class="men"
class="mainmen"
class="MyMen"
class="menSuper"
class="MyMenSuper"
etc
I need someting like strpos($string,'class="%men%') where % could be anything.
Best,
Martti
Try using preg_match
if (preg_match("/men/i", $class)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
Look at this link for more information http://www.php.net/preg_match
Edit :
So you can do like this (Inspired from the answer of Marius.C)
$string = '<div class="menlook">Some text</div>';
if (preg_match('/class="(.*)men(.*)"/i', $string)) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
Store class "men" as string in variable like "$your_class"
then use preg_match like this:
if(preg_match('/men/i', $your_class)) {
echo "Men Class Found!";
}
ref: http://php.net/preg_match
or using strpos:
if(strpos(strtolower($your_class),'men')!==false) {
echo "Men Class Found!";
}
ref: http://www.w3schools.com/php/func_string_strpos.asp
Use strpos two times,
if(strpos($string,'class=') !== false && strpos($string,'men') !== false){
echo "true";
}
Note: strpos is much faster than preg_match.
This is possible without regular expressions which are quite slow
stristr($string, 'men')
I believe that you need something like this:
preg_match_all('/class="(.*)men(.*)"/i', $string, $matches);
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 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';
}