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
Post a Comment