简体   繁体   中英

Having problems with strpos in php

I run a dog pedigree site and trying to show images based on whether dog is a champion.

Now you get different champions Italian, French etc.

The first line works but the second does not for the dog whose name begins with IT/CH?

<?php 
    if (strpos($dog_name, "CH", 0)=== false) 
        echo '<img src="img/first.png"></img>';
?>

This above works if the dog has just CH at the front of name as CH Dudley at Whereof

This one below does not work why?

<?php 
    if (strpos($dog_name, "IT/CH", 0)=== false) 
        echo '<img src="img/uk_champ.png"></img>';
?>

Dogs name is IT/CH Famersco Hadley.

I'm really stuck on this - any help would be useful.

I believe it is working perfectly. In the 1st example it returns false as the $dog_name not starts with 0. But in the 2nd example it is not false as it is starting with "IT/CH"

If you want to see a match anywhere in the string just use strpos without the 3rd parameter

if (strpos($dog_name, "CH")=== false) 
echo '<img src="img/uk_champ.png"></img>';

and

if (strpos($dog_name, "IT/CH")=== false) 
echo '<img src="img/uk_champ.png"></img>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM