php - Testing for matches within a string -


i have script grabs of hash tags string , stores them in array, happens next determined whether or not there hash tags in string. how create if statement run code if there indeed hash tags in string being tested.

here have tried:

<?php  $string= "went awesome bike";  echo $string . "</br></br>";  preg_match_all('/#(\w+)/',$string, $matches);   if ($matches != 0) {      foreach ($matches[1] $tag) {          echo $tag . "</br></br>";      }  } else {      echo "there no tags here!!";  }   ?> 

i cant echo out failure message? doing wrong?

thanks!

preg_match_all() return how many matches made (including 0), or boolean false on failure, simple:

$cnt = preg_match_all(...); if (($cnt !== false) && ($cnt > 0)) {     ... found ... } 

will do. note !==. necessary distinguish between true boolean false, , simple integer 0, both of test equal when using standard non-strict == logic.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -