Ensure that word exists in variable

Hello everybody…

i select website addresses from database all that i need is ensure that is ‘.com’ exists in variable or not

for example:
if $x[1]=‘www.yahoo.com’ –> return true.

but if $x[1]=‘www.yahoo’ –> return false with error message website is not entered correctly.

thanks in advance

Take a peek at, [fphp]strstr/fphp, [fphp]stristr/fphp & [fphp]strpos/fphp. :wink:

Try this-

<?php
$url= ‘www.yahoo.com’;
if(stristr($url, ‘com’)}
{
//yes

}else{

//no}

?>

Many ways to do it. If you only need to check for .com and you are sure it’s on the end of the string:


if ('.com' === substr($string, -4)) {
    // do something
}

But you might as well need to check for other extensions. Or you might need to validate the domain name is valid. I recommend Zend_Validate for that:


      $validator = new Zend_Validate_Hostname();
      if ($validator->isValid($hostname)) {
          // hostname appears to be valid
      } else {
          // hostname is invalid; print the reasons
          foreach ($validator->getMessages() as $message) {
              echo "$message\
";
          }
      }

Code snippet taken from here: http://framework.zend.com/manual/en/zend.validate.set.html