I am trying to validate whether or not a string contains and starts with BA700. I have tried using the preg_match() function in PHP but I have not had any luck. My code is below:
preg_match('/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/', $search))
This does not work unfortunately. Any ideas?
UPDATES CODE:
$needle = 'BA700';
$haystack = 'BA70012345';
if (stripos($haystack, $needle)) {
echo 'Found!';
}
This does not work for me either
Here is how to correctly use stripos
if (stripos($haystack, $needle) !== false) {
echo 'Found!';
}
Maybe I am taking this a little too literally, but:
if (strncmp($string, 'BA700', 5) === 0) {
// Contains and begins with 'BA700'
}
If the BA700 is case-insensitive then:
if (strncasecmp($string, 'ba700', 5) === 0) {
// Contains and begins with 'ba700'
}
There should not be much more to it than that.
The regular expression, in case you want to know, is:
if (preg_match('/^BA700/', $string) === 1) {
// Contains and begins with 'ba700'
}
Try with substr like
$needle = 'BA700';
$haystack = 'BA70012345';
if(substr($haystack, 0, 4) == $needle) {
echo "Valid";
} else {
echo "In Valid";
}
You can also check regards with the case by changing both of them in either UPPER or LOWER like
if(strtoupper(substr($haystack, 0, 4)) == $needle) {
echo "Valid";
} else {
echo "In Valid";
}
Related
I'm trying to check if a specific character is not present in a string in my code and apparently php doesn't care about anything and always gets inside the if
foreach($inserted as $letter)
{
if(strpos($word, $letter) !== true) //if $letter not in $word
{
echo "$word , $letter, ";
$lives--;
}
}
In this case $word is "abc" and $letter is "b", I've tried changing a lot of random things like from true to false and things like that but I can't get it, can anyone help me please?
Changing the way you validate should fix it, like below:
foreach($inserted as $letter)
{
//strpos returns false if the needle wasn't found
if(strpos($word, $letter) === false)
{
echo "$word , $letter, ";
$lives--;
}
}
if(strpos($word, $letter) === false) //if $letter not in $word
{
echo "$word , $letter, ";
$lives--;
}
also, be careful to check explicitly against false, strpos can return 0 (a falsey value) if the match is in the 0th index of the string...
for example
if (!strpos('word', 'w') {
echo 'w is not in word';
}
would output the, possibly confusing, message 'w is not in word'
I found this example on stackoverflow:
if (strpos($a,'are') !== false) {
echo 'true';
}
But how do I make it search for two words. I need something like this: if $a contains the words "are" or "be" or both echo "contains";
I tried xor and ||
Just check both words separately and use the boolean or-operator to check if either one or both are contained in $a:
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
echo "contains";
}
Note that due to the or-operator, the second check (for 'be') is not performed if the first one already showed that $a contains 'are'.
An alternative: Searches any length of words in the longer string.
Since you've haven't picked an answer from all the strpos answers (most of which should work with just two words, try this function of mine which exceeds word limits. It can find any varying length of words from the longer string (but doesn't use strpos). I think with strpos, you would have to know the number of words to determine how many || you should use or make use of a loop (sort of). This method eliminates all that and gives you a more flexible way to reuse your codes. I thinks codes should be flexible, reusable and dynamic. Test it and see if it does what you want!
function findwords($words, $search) {
$words_array = explode(" ", trim($words));
//$word_length = count($words_array);
$search_array = explode(" ", $search);
$search_length = count($search_array);
$mix_array = array_intersect($words_array, $search_array);
$mix_length = count($mix_array);
if ($mix_length == $search_length) {
return true;
} else {
return false;
}
}
//Usage and Examples
$words = "This is a long string";
$search = "is a";
findwords($words, $search);
// $search = "is a"; // returns true
// $search = "is long at"; // returns false
// $search = "long"; // returns true
// $search = "longer"; // returns false
// $search = "is long a"; // returns true
// $search = "this string"; // returns false - case sensitive
// $search = "This string"; // returns true - case sensitive
// $search = "This is a long string"; // returns true
$a = 'how are be';
if (strpos($a,'are') !== false || strpos($a,'be') !== false) {
echo 'contains';
}
Try:
if (strpos($a,'are') !== false || strpos($a,'be') !== false)
echo 'what you want';
if ((strpos($a,'are') !== false) || (strpos($a, 'be') !==false) {
echo 'contains';
}
Is this what you want?
if ((strpos($a,'are') !== false) || (strpos($a,'be') !== false)) {
echo 'contains';
}
if (strpos($a,'are') !== false || strpost($a, 'be') !== false) {
echo "contains";
}
Brain Candy:
If the first one returns true, it'll skip the second check. So both can be true. If the first one is false, ONLY then will it check the second. This is called a short circuit.
if(strstr($a,'are') || strstr($a,'be')) echo 'contains';
Hum, like this?
if (strpos($a,'are') || strpos($a, 'be') {
echo 'contains';
}
I need a function in php that will work in this way.
$string = "blabla/store/home/blahblah";
If in $string you find /store/ then do this, else do that.
How can I do it?
Thanks!
you're looking for strpos() function
$string = "blabla/store/home/blahblah";
if (preg_match("|/store/|", $string)){
//do this
}
else{
//do that
}
or
$string = "blabla/store/home/blahblah";
if (false !== strpos($string, "/store")){
//do this
}
else{
//do that
}
if (strpos($string, "/store/") !== false) {
// found
} else {
// not found
}
Try using the strrpos function
e.g.
$pos = strrpos($yourstring, "b");
if ($pos === true) { // note: three equal signs
//string found...
}
Seems like you're looking for the stristr() function.
$string = "blabla/store/home/blahblah";
if(stristr($string, "/store/")) { do_something(); }
I need to search a string for any occurances of another string in PHP. I have some code that I've been playing with, but it doesn't seem to be working.
Here is my code:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos == false) {
echo "allow";
exit;
} else {
echo "deny";
exit;
}
}
I have tried some of the options below, but it still does not find the match. Here is what I'm searching:
I need to find:*
blah
In:
http://blah.com
Nothing seems to find it. The code works in regular sentences:
Today, the weather was very nice.
It will find any word from the sentence, but when it is all together (in a URL) it can't seem to find it.
When checking for boolean FALSE in php, you need to use the === operator. Otherwise, when a string match is found at the 0 index position of a string, your if condition will incorrectly evaluate to true. This is mentioned explicitly in a big red box in the php docs for strpos().
Also, based on a comment you left under another answer, it appears as though you need to remove the exit statement from the block that allows access.
Putting it all together, I imagine your code should look like this:
while (list($key, $val) = each($keyword)) {
$pos = strpos($location, $val);
if($pos === false) { // use === instead of ==
echo "allow";
} else {
echo "deny";
exit;
}
}
Update:
With the new information you've provided, I've rewritten the logic:
function isAllowed($location) {
$keywords = array('blah', 'another-restricted-word', 'yet-another-restricted-word');
foreach($keywords as $keyword) {
if (strpos($location, $keyword) !== FALSE) {
return false;
}
}
return true;
}
$location = 'http://blah.com/';
echo isAllowed($location) ? 'allow' : 'deny';
Does anyone know how can I do a string check inside a string?
for example:
$variable = "Pensioner (other)";
If I want to check whether $variable contain the word 'Pensioner', how can I do it in PHP? I have tried the following code in php, but it's always return me false :(
$pos = strripos($variable,"Pensioner");
if($pos) echo "found one";
else echo "not found";
In the manual, the example uses a === for comparison. The === operator also compares the type of both operands. To check for 'not equal', use !==.
Your search target 'Pensioner' is at position 0, and the function returns 0, which equal false, hence if ($pos) failed all the time. To correct that, your code should read:
$pos = strripos($variable,"Pensioner");
if($pos !== false) echo "found one";
else echo "not found";
Update:
You are using the reverse function strripos, you need to use stripos.
if (stripos($variable, "Pensioner") !== FALSE){
// found
}
else{
// not found
}
This should do:
if (strripos($variable, "Pensioner") !== FALSE){
// found
}
else{
// not found
}
The strict type comparison (!==) is important there when using strpos/stripos.
The problem with strripos and its siblings is that they return the position of the substring found. So if the substring you're searching happens to be at the start, it returns 0 which in a boolean test is false.
Use:
if ( $pos !== FALSE ) ...
$variable = 'Pensioner (other)';
$pos = strripos($variable, 'pensioner');
if ($pos !== FALSE) {
echo 'found one';
} else {
echo 'not found';
}
^ Works for me. Note that strripos() is case insensitive. If you wanted it to be a case-sensitive search, use strrpos() instead.