Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you echo 'if includes something'?

Status
Not open for further replies.

meeble3

Programmer
Nov 8, 2004
52
GB
Hello.

When $myrow2[0] is pulled from the database, if $myrow2[0] has 'ab' in it, I want to echo one thing else echo something else.

How do you do this please?
 
Like this:

Code:
if ( strpos( $myrow2[0], 'ab' ) === false ) {
    echo "string not found";
} else {
    echo "'ab' was found";
}

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
There are several string handling functions that can do what you want. cLFlaVA has shown the use of strpos - which indirectly tells you that the "needle" is contained in the haystack by returning its relative position.
There is also strstr($hastack,$needle) which returns the part of the haystack beginning with the first occurrence of needle.

The logic in both cases is the same. If the function returns something else than "false" (which menas not found) then you can conclude that the needle was part of the haystack.

For more complicated patterns than simple strings PHP offers PCRE (Perl COmpatible Regular Expressions).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top