real_illusions Posted February 9, 2011 Share Posted February 9, 2011 Hi all,I have an array of certain words and I want to see how many times they are used within a string.Was thinking of the strpos() function, but that only returns the first instance of a word/value within the string.Is there an easy way of doing this? Link to comment Share on other sites More sharing options...
iwato Posted February 9, 2011 Share Posted February 9, 2011 Is there an easy way of doing this?This would be a good place to start: preg_match_all().Roddy Link to comment Share on other sites More sharing options...
real_illusions Posted February 9, 2011 Author Share Posted February 9, 2011 Cant quite get my head round RegEx.Basically need a way of finding how many heading tags are on a web page (<h1> thru to <h6>.Would a regex pattern like this "h.[1-6]" do the trick or is that completely in the wrong direction? Link to comment Share on other sites More sharing options...
real_illusions Posted February 9, 2011 Author Share Posted February 9, 2011 Nevermind...think I got it.$data = file_get_contents($url);$pattern = "/h1|h2|h3|h4|h5|h6]/";preg_match_all($pattern, $data, $headings);Now, how to count the results in the array.... Link to comment Share on other sites More sharing options...
real_illusions Posted February 9, 2011 Author Share Posted February 9, 2011 solved :)This echos out a multidimensional array, so the normal count ($array); doesn't work. You need to do this: count( $array, COUNT_RECURSIVE ); instead. Link to comment Share on other sites More sharing options...
boen_robot Posted February 9, 2011 Share Posted February 9, 2011 To avoid matching literal text that says "h6", you might want to include at least the "<" in your regex, like: $pattern = "/\<(h1|h2|h3|h4|h5|h6)/"; But keep in mind the $headings array would be altered a little by that change. Make sure you count it properly (var_dump it, inspect it... you'll see). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.