1

In php is there a way i can check if a string includes a value. Say i had a string "this & that", could i check if that included "this". Thanks

UPDATED:

$u = username session

function myLeagues2($u)
{
     $q = "SELECT * FROM ".TBL_FIXTURES." WHERE `home_user` = '$u' GROUP BY `compname` ";
   return mysql_query($q, $this->connection);

}

That code returns if there is an exact match in the database. But i put two usernames together like "username1 & username2" and need to check if the username session is present in that field.

Is that a way to do this? It obviously woudn't equal it.

ANOTHER UPDATE:

If i use the like %username% in the sql, will it appear if its just the username. So rather than Luke & Matt being shown, if its just Luke will that show too? I dont want it to, i just want things that arent. Can you put != and LIKE so you only get similar matches but not identical?

Cheers

1
  • 1
    Now this is a completely different subject. MySQL has nothing to do with PHP. Commented Jul 23, 2010 at 19:44

5 Answers 5

7

Use strstr or strpos functions. Although there are regex ways too but not worth for such trivial task there.

Using strstr

if (strstr('This & That', 'This') !== false)
{
   // found
}

Using strpos

if (strpos('This & That', 'This') !== false)
{
   // found
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hey thanks, could you give a brief example how I could use this?
Thanks for the updated answer. Can you check my updated original post, what i want to do is slightly different, dont know if there is another way to do this.
@Luke: I don't understand your update, you should post more code about how you want to compare with as you say "username1 & username2". It is not clear to me, you should post more code or explain a bit more.
1

I like stristr the best because it is case insensitive.

Usage example from php.net:

$email = '[email protected]';
echo stristr($email, 'e'); // outputs [email protected]
echo stristr($email, 'e', true); // As of PHP 5.3.0, outputs US

based on your update

I think you want to use the like statement

SELECT * FROM table 
WHERE home_user LIKE "%username1%" OR home_user LIKE "%username2%"
GROUP BY compname

The % Is your wild card.

Comments

0
<?php>
$string = 'this & that';
if (strpos($string, 'this') === FALSE) {
    echo 'this does not exist!';
} else {
    echo 'this exists!';
}
?>

What's noteworthy in the check is using a triple equal (forget the actual name). strpos is going to return 0 since it's in the 0th position. If it was only '== FALSE' it would see the 0 and interpret it as FALSE and would show 'this does not exist'. Aside from that this is about the most efficient way to check if something exists.

Comments

-2
preg_match('/this/','this & that');

This returns the number of matches, so if it's 0, there were no matches. It will however stop after 1 match.

1 Comment

Yeah, good point. I just threw it out as a second option because we don't know what the OP's ultimate goal is.
-2

Regular expressions can be checked with preg-match.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.