Implement a counter with "preg_match_all" function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marco

    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>ph p</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>progra mming</H3>"
    folder (2 items).


    <TITLE>Bookmark s</TITLE>
    <H1>Bookmarks </H1>

    <DT><H3>php</H3>
    <DL><p>
    <DT><A HREF="http://www.w3schools.c om/php/default.asp"
    ID="rdf:#$ScZXl 3">PHP Tutorial</A>
    <DT><A HREF="http://www.php.net/" ID="rdf:#$TcZXl 3">PHP: Hypertext
    Preprocessor</A>
    <DT><A HREF="http://www.phpdig.net/" ID="rdf:#$TfZXl 3">PhpDig.ne t -
    Web Spider and Search Engine</A>
    </DL><p>
    <DT><H3>program ming</H3>
    <DL><p>
    <DT><A HREF="http://www.codebrain.c om/java/gutenberg/index.html"
    ID="rdf:#$qI7Ab 2">GUTENBERG APPLET</A>
    <DT><A HREF="http://www.w3schools.c om/" ID="rdf:#$UcZXl 3">W3Schools
    Online Web Tutorials</A>
    </DL><p>


    // php file ----------------------------------------------------------------------------

    $file = file_get_conten ts("bookmarks.h tml"); // open & reads the
    bookmarks document

    preg_match_all( "/\">*(.*?)<\/*h3>\s*<dl><p>\ s*.*<dt><a\s*hr ef=*\"(.*?)\".* \">(.*?)<\/*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

    #2
    Re: Implement a counter with &quot;preg_matc h_all&quot; 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.c om/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 => "programmin g",
    2 => "http://www.codebrain.c om/java/gutenberg/index.html",
    3 => "GUTENBERG APPLET",
    4 => "http://www.w3schools.c om/",
    5 => "W3Schools Online Web Tutorials",
    )
    );

    I hope, I could help you.


    Comment

    • Christopher-Robin

      #3
      Re: Implement a counter with &quot;preg_matc h_all&quot; function

      notice the space inbetween

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


      Comment

      • Victor Rades

        #4
        Re: Implement a counter with &quot;preg_matc h_all&quot; 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.rade s.com.br/prg/php/scripts/moz-bookmarks/>

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

        alphacentauri@d oglover.com (marco) wrote in message news:<ff722986. 0409160144.72ae 94e3@posting.go ogle.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>ph p</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>progra mming</H3>"
        > folder (2 items).
        >
        >
        > <TITLE>Bookmark s</TITLE>
        > <H1>Bookmarks </H1>
        >
        > <DT><H3>php</H3>
        > <DL><p>
        > <DT><A HREF="http://www.w3schools.c om/php/default.asp"
        > ID="rdf:#$ScZXl 3">PHP Tutorial</A>
        > <DT><A HREF="http://www.php.net/" ID="rdf:#$TcZXl 3">PHP: Hypertext
        > Preprocessor</A>
        > <DT><A HREF="http://www.phpdig.net/" ID="rdf:#$TfZXl 3">PhpDig.ne t -
        > Web Spider and Search Engine</A>
        > </DL><p>
        > <DT><H3>program ming</H3>
        > <DL><p>
        > <DT><A HREF="http://www.codebrain.c om/java/gutenberg/index.html"
        > ID="rdf:#$qI7Ab 2">GUTENBERG APPLET</A>
        > <DT><A HREF="http://www.w3schools.c om/" ID="rdf:#$UcZXl 3">W3Schools
        > Online Web Tutorials</A>
        > </DL><p>
        >
        >
        > // php file ----------------------------------------------------------------------------
        >
        > $file = file_get_conten ts("bookmarks.h tml"); // open & reads the
        > bookmarks document
        >
        > preg_match_all( "/\">*(.*?)<\/*h3>\s*<dl><p>\ s*.*<dt><a\s*hr ef=*\"(.*?)\".* \">(.*?)<\/*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]

        Comment

        • Victor Rades

          #5
          Re: Implement a counter with &quot;preg_matc h_all&quot; function

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

          Comment

          Working...