By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,239 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,239 IT Pros & Developers. It's quick & easy.

Implement a counter with "preg_match_all" function

P: n/a
Hello,

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).
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>

<DT><H3>php</H3>
<DL><p>
<DT><A HREF="http://www.w3schools.com/php/default.asp"
ID="rdf:#$ScZXl3">PHP Tutorial</A>
<DT><A HREF="http://www.php.net/" ID="rdf:#$TcZXl3">PHP: Hypertext
Preprocessor</A>
<DT><A HREF="http://www.phpdig.net/" ID="rdf:#$TfZXl3">PhpDig.net -
Web Spider and Search Engine</A>
</DL><p>
<DT><H3>programming</H3>
<DL><p>
<DT><A HREF="http://www.codebrain.com/java/gutenberg/index.html"
ID="rdf:#$qI7Ab2">GUTENBERG APPLET</A>
<DT><A HREF="http://www.w3schools.com/" ID="rdf:#$UcZXl3">W3Schools
Online Web Tutorials</A>
</DL><p>
// php file ----------------------------------------------------------------------------

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

preg_match_all("/\">*(.*?)<\/*h3>\s*<dl><p>\s*.*<dt><a\s*href=*\"(.*?)\".*\">(. *?)<\/*a>/i",
$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
advance!

AlphaC
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi

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

preg_match_all('|<h3>(.*?)</h3>.*?<dl>(?:.*?<a
+href="(.*?)".*?>(.*?)</a>)*.*?</dl>|musi',$file,$list,PREG_SET_ORDER);

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!!):

$list=array(
0 => array(
0 => "...", // 1st full-pattern-match
1 => "php",
2 => "http://www.w3schools.com/php/default.asp",
3 => "PHP Tutorial",
4 => "http://www.php.net/",
5 => "PHP: Hypertext Preprocessor",
6 => "http://www.phpdig.net/",
7 => "PhpDig.net - Web Spider and Search Engine",
),
1 => array(
0 => "...", // 2nd full-pattern-match
1 => "programming",
2 => "http://www.codebrain.com/java/gutenberg/index.html",
3 => "GUTENBERG APPLET",
4 => "http://www.w3schools.com/",
5 => "W3Schools Online Web Tutorials",
)
);

I hope, I could help you.
Jul 17 '05 #2

P: n/a
notice the space inbetween

....<a +href="(.*?)".*?>...
Jul 17 '05 #3

P: n/a
Hi, I write the script.
I use the bookmarks from Mozilla (I think you too).
You can see it in action at
<http://www.victor.rades.com.br/prg/php/scripts/moz-bookmarks/>

For now the code are unavaliable :( I'll make a better code and post
here.

al***********@doglover.com (marco) wrote in message news:<ff**************************@posting.google. com>...
Hello,

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).
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>

<DT><H3>php</H3>
<DL><p>
<DT><A HREF="http://www.w3schools.com/php/default.asp"
ID="rdf:#$ScZXl3">PHP Tutorial</A>
<DT><A HREF="http://www.php.net/" ID="rdf:#$TcZXl3">PHP: Hypertext
Preprocessor</A>
<DT><A HREF="http://www.phpdig.net/" ID="rdf:#$TfZXl3">PhpDig.net -
Web Spider and Search Engine</A>
</DL><p>
<DT><H3>programming</H3>
<DL><p>
<DT><A HREF="http://www.codebrain.com/java/gutenberg/index.html"
ID="rdf:#$qI7Ab2">GUTENBERG APPLET</A>
<DT><A HREF="http://www.w3schools.com/" ID="rdf:#$UcZXl3">W3Schools
Online Web Tutorials</A>
</DL><p>
// php file ----------------------------------------------------------------------------

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

preg_match_all("/\">*(.*?)<\/*h3>\s*<dl><p>\s*.*<dt><a\s*href=*\"(.*?)\".*\">(. *?)<\/*a>/i",
$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
advance!

AlphaC

Jul 17 '05 #4

P: n/a
sorry, correct link: <http://www.victor.rades.com.br/prg/php/scripts/moz-bookmark/>
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.