Search code examples
phpstrpos

How to use strpos() to check a variable's path?


I have a home page that shows some uploaded images.

I take it from the database and I use strpos() to check the URL due to directory problems, and it works fine:

if(strpos($row['cImage'],"http://") === FALSE){
    echo "<img src='serintercement/".$row['cImage']."' style='width: 107px; height:102px;' />";    
}else{
    echo "<img src='".$row['cImage']."' style='width: 107px; height:102px;' />";    
}

I need to use the same logic in a page that shows the clicked image, but it has a variable for it and I'm struggling to fix this since it's a different way to write:

<img src='<?php echo $resultData->cImage; ?>'/>

I can't fix the directory problem. How can I use strpos() similarly for this second code?


Solution

  • You can do it like this.

    if(strpos($resultData->cImage,"http://") === FALSE){
        echo "<img src='serintercement/".$resultData->cImage."' style='width: 107px; height:102px;' />";    
    }else{
        echo "<img src='".$resultData->cImage."' style='width: 107px; height:102px;' />";    
    }
    

    Better option is you can define a function like this and call it

    checkImage($row['cImage']);//to be called in your first page
    checkImage($resultData->cImage);//to be called in your second page
    function checkImage($image)
    {
        if(strpos($image,"http://") === FALSE){
            echo "<img src='serintercement/".$image."' style='width: 107px; height:102px;' />";    
        }else{
            echo "<img src='".$image."' style='width: 107px; height:102px;' />";    
        }
    }