Search code examples
phphtmlvariablesstrpos

String position find opening div in string


I try to find '<video' string in this variable that contains html

$string="<video autoplay="autoplay" loop="" muted="" poster="" preload="auto" style="width: 354.09836065574px; height: 600px; max-height:600px;;display:block;margin:0 auto;"><source src="" type="video/mp4" /> <source src="" type="video/webm" />
<div class="badge-item-animated-img">&nbsp;</div> "

So I use :

if (strpos($string, '<video')){
   echo $string;
}

Why doesn't this work?


Solution

  • Because strpos returns the current position of the tag 'video', which is 0 in your case. In PHP this is the same as false. Try this instead:

    if (strpos($string, '<video') !== false){
         echo $string;
     }