Search This Blog

preg_match() or search a string / substring in php

<?php

$string='hello world';
if(preg_match("/hel/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output :word or characted found


<?php

$string='hello world';
if(preg_match("/z/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>

output :word or characted not found

<?php

$string='hello world';
$searchstring='hel';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output :word or characted found
<?php

$string='hello world';
$searchstring='z';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>

output :word or characted not found
<?php

$string='hello world';
$searchstring='hello';
if(preg_match("/$searchstring/",$string))
  {
     echo 'word or characted found';
  }
else
  {  echo 'word or characted not found';
  }

?>
output : word or characted found

Share Links

Related Posts Plugin for WordPress, Blogger...