473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Implement a counter with "preg_match_all" function

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
4 3094
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
notice the space inbetween

....<a +href="(.*?)".*?>...
Jul 17 '05 #3
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
sorry, correct link: <http://www.victor.rades.com.br/prg/php/scripts/moz-bookmark/>
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: futureofphp | last post by:
I need to access a website, Fetch the data, and then logout. Please tell me where I am wrong. I dont think its logging out properly. <? include "Snoopy.class.php"; $snoopy = new Snoopy; ...
15
by: NurAzije | last post by:
I have written a code and can't figure what is wrong, the code will read a content of a file and cut all the emails from it,then echo them, but I have a problem with '"' charecter I think.. Take a...
6
by: Michael | last post by:
How can I get X (or another value) from this string "asdfsadfX", basically i want to get what ever is in between the tags and place them in a variable called $www , can anyone help?
1
by: Suhas Dhoke | last post by:
Hello Raja. Where is the constructor of the class orkut ?? On Oct 24, 1:40 pm, Raja <RajaSa...@gmail.comwrote:
2
by: jeddiki | last post by:
Hello, I want to create my own index of websites based on my criteria rather than big G's. For example, I might like to index websites according to what they have in their "author" Meta tag (...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.