example:

<img src="img.jpg" alt=""   />

<a href="#" alt=""   />

i want to do someting if the string="alt" exist in the img tag
and someting else if the alt exist in the href tag

Here's how I would do it (psudo code):

if(strpos($tagString, "alt=") !== false && strpos($tagString, "<img") !== false)
{
     //do img stuff
}
else if((strpos($tagString, "alt=") !== false && strpos($tagString, "<a href=") !== false)
{
      //do href stuff
}
else
{
      //the alt tag either doesn't exist or doesn't exists with img or href
}

or you could:

if(strpos($tagString, "alt=") !== false)
{

if(strpos($tagString, "<img") !== false)
{
     //do img stuff
}
else if(strpos($tagString, "<a href=") !== false)
{
      //do href stuff
}
else
{
      //the alt tag doesn't exists with img or href
}

}
else
{
     //alt tag doesn't exist
}

I hope that helps