physaux Posted October 17, 2009 Share Posted October 17, 2009 Hey guys, i read http://weblogtoolscollection.com/regex/regex.php, about an hour ago, so i am really new to regex. My question is, if i had a string, and i wanted to count how many times "<b>" appears, which function would i call for this? I know how to create the "patterns" and such, just not which function to use. ex: preg_something? Any pointers greatly appreciated!! Link to comment https://forums.phpfreaks.com/topic/178051-counting-html-tags-how-new-at-regex/ Share on other sites More sharing options...
cags Posted October 17, 2009 Share Posted October 17, 2009 Personally, if the thing you wish to count is as simple as that and doesn't require a complex pattern match, I'd use substr_count. But to count instances of a complex pattern I'd probably use the function preg_match_all. Link to comment https://forums.phpfreaks.com/topic/178051-counting-html-tags-how-new-at-regex/#findComment-938846 Share on other sites More sharing options...
JAY6390 Posted October 18, 2009 Share Posted October 18, 2009 I'd go along with what cags has said. If you want to match using the preg_match_all I'd use '%<b(\b[^>]*)?>%i' as your regex Link to comment https://forums.phpfreaks.com/topic/178051-counting-html-tags-how-new-at-regex/#findComment-939193 Share on other sites More sharing options...
thebadbad Posted October 18, 2009 Share Posted October 18, 2009 I'd go along with what cags has said. If you want to match using the preg_match_all I'd use '%<b(\b[^>]*)?>%i' as your regex Just a small note; you can remove the optional capture since the word boundary will match even if the following character class (quantized with the asterisk) matches zero times. '~<b\b[^>]*>~i' Link to comment https://forums.phpfreaks.com/topic/178051-counting-html-tags-how-new-at-regex/#findComment-939375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.