Hiigaran
Hiigaran

Reputation: 831

Displaying an image via PHP if HTML contains a specific word/phrase

I thought this would have been simple enough using strpos, so I must be oblivious to a horribly obvious mistake. I have this code here:

<div class="text" style="text-align:center;border:2px solid #bbb;font-size:12px;padding:0.2%;">
    <?php
        $root = realpath($_SERVER['DOCUMENT_ROOT']);
        include "$root/scripts/display.php";
    ?>
</div>

Within the included file is a simple array that displays a random string from it:

$data=array("random string","something","something else","and so on");
$info=$data[rand(0,count($data)-1)];
echo $info;

What I want to do is to display an image that is relevant to a few of the random strings that are echoed. I thought I could do this with an if check for strpos as, for example "random" in $info, such that:

...
    ?>
</div>
<?php if(strpos($info,"random") !== false){?>
    <img src=...>
<?php } ?>

Problem is, this displays the image, even if "random" was not echoed.

I have a feeling this might have something to do with $info being checked, right?

Upvotes: 0

Views: 156

Answers (1)

user3470953
user3470953

Reputation: 11335

can you please check with the following if it solves you problem

$data=array("1","2","3","4","5");
   $info = array_rand($data, count($data));
   print_r($info);

    <?php if(in_array("4", $info)){?>
        <img src=...>
    <?php } ?>

Upvotes: 1

Related Questions