Connecting Tech Pros Worldwide Forums | Help | Site Map

Implement a counter with "preg_match_all" function

marco
Guest
 
Posts: n/a
#1: Jul 17 '05
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

Christopher-Robin
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Implement a counter with "preg_match_all" function


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.


Christopher-Robin
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Implement a counter with "preg_match_all" function


notice the space inbetween

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


Victor Rades
Guest
 
Posts: n/a
#4: Jul 17 '05

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
<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.

alphacentauri@doglover.com (marco) wrote in message news:<ff722986.0409160144.72ae94e3@posting.google. com>...[color=blue]
> 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[/color]
Victor Rades
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Implement a counter with "preg_match_all" function


sorry, correct link: <http://www.victor.rades.com.br/prg/php/scripts/moz-bookmark/>
Closed Thread