Jump to content

Counting HTML Tags: how? -new at REGex.


Recommended Posts

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

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.

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'

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.