Implement a counter with "preg_match_all" function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm putting together a php webpage which is parsing my (.html)
bookmarks list. I want to give them a new lay-out with php and CSS.

My question is:
How can I make a function that counts and strores the number of
bookmark links of each seperate(!) bookmarkfolder in a variable?

For example: the "<DT><H3>php</H3>" (see the html sample underneath)
is the php folder and I want to know how many links (in this case 3)
are in that folder and the same for the "<DT><H3>programming</H3>"
folder (2 items).


<DT><A HREF=" "
ID="rdf:#$ScZXl3">PHP Tutorial</A>
<DT><A HREF=" /" ID="rdf:#$TcZXl3">PHP: Hypertext
<DT><A HREF=" /" ID="rdf:#$TfZXl3"> -
Web Spider and Search Engine</A>
<DT><A HREF=" "
<DT><A HREF=" /" ID="rdf:#$UcZXl3">W3Schools
Online Web Tutorials</A>

// php file

$file = file_get_contents("bookmarks.html"); // open & reads the
bookmarks document

$file, $links); // picking out (1-the folder names =$links[1], 2-the
links =$links[2], 3-the linktitles =$links[3]

print "<pre>";
print_r($links[2]); // prints all the 5 links
print "</pre>";


I hope somebody have some smart ideas about this. Already thanks in


Re: Implement a counter with "preg_match_all" function

Hi, I write the script.
I use the bookmarks from Mozilla (I think you too).
You can see it in action at

For now the code are unavaliable :( I'll make a better code and post
here. (marco) wrote in message
Quoted text here. Click to load it
Quoted text here. Click to load it

Re: Implement a counter with "preg_match_all" function

sorry, correct link:

Re: Implement a counter with "preg_match_all" function


Seems that you just need a correct preg-pattern. Try this one:


The clue is the (?: ... )* parenthesis. <?:> at the beginning indicates not
to be a pattern-match to be returned, but to repeat (see the <*> after the
right perenthesis) matching as often as possible.

The resulting $list is an array of all bookmark-folders, where $list[0]
contains an array of the first folder, $list[1] contains an array of the
second folder, and so on.
Each $list[..] is an array, where
  $list[..][1] is the folder-title,
  $list[..][x] is the link-url,
  $list[..][y] is the link-name.
    x is any even number (depends on how many links there are)
    y is any odd number (depends on how many links there are)

So, your example would result in this (I didn't test it!!):

  0 => array(
    0 => "...", // 1st full-pattern-match
    1 => "php",
    2 => " ",
    3 => "PHP Tutorial",
    4 => " /",
    5 => "PHP: Hypertext Preprocessor",
    6 => " /",
    7 => " - Web Spider and Search Engine",
  1 => array(
    0 => "...", // 2nd full-pattern-match
    1 => "programming",
    2 => " ",
    4 => " /",
    5 => "W3Schools Online Web Tutorials",

I hope, I could help you.

Re: Implement a counter with "preg_match_all" function

notice the space inbetween

....<a +href="(.*?)".*?>...

Site Timeline