Results 1 to 10 of 10

Thread: Using PHP to create links on a page

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Question Using PHP to create links on a page

    My page is split into 2 divs - #content and #sidebar and I'm splitting this into 2 php pages.

    What I'd like to do is insert a function into #sidebar (sidebar.php) where it will scan #content (content.php) for a
    HTML Code:
    <a name="#1"></a>
    tag and then output a
    HTML Code:
    <a href="1">1</a>
    into sidebar.php

    Can anyone help start me off on this - I know I need to put the results into an array and then output them somehow, but I'm not sure how to correctly scan content.php. (By the way content.php will just be a static page and not contain any dynamic content).

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Using PHP to create links on a page

    you'll just need to open the file (not locally, however, because it's a PHP file and we'll assume that the content is not just pure HTML for this purpose) using a function like file_get_contents(), and you can apply a simple regular expression with preg_match_all() to find all of the anchor tags that match a certain pattern (with a specific class, or whatever you might want -- or all of them). this is a fairly simple example that accomplishes the basics of what you want:

    PHP Code:
    <xmp>
    <?php
      
      
    // the contents of the file
      
    $file file_get_contents("http://www.vbforums.com"); // 380ish total matches for vbforums.com, fyi!

      // regular expression to find ALL anchor tags
      
    $pattern '/<a.*?href="([^"]+)".*?>(.*?)<\/a>/i';
      
      
    // search!
      
    preg_match_all($pattern$file$matches);
      
    print_r($matches); // displaying the array [for debugging]

      /*  $matches array:
       *
       *  key 0: holds entire matched strings
       *  key 1: holds all values for the first match (URL)
       *  key 2: holds all matches for the second match (label)
       */
       
      // iterate the new array
      
    for($i 0$i count($matches[0]); $i++){
      
        
    // print a new URL
    ?>
    <a class="sideURL" href="<?php echo $matches[1][$i]; ?>"><?php echo $matches[2][$i]; ?></a>
    <?php
      
      
    }
    ?>
    if any of this code doesn't make sense to you (aside from perhaps the regular expression), please feel free to ask questions! feel free to ask about the regular expression, as well, it's just not something I'd expect someone to just understand right away.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Using PHP to create links on a page

    Thank you so much for this! I'll test it out on my dummy site and let you know how it goes.

  4. #4
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Using PHP to create links on a page

    This is kinda threadjacking here. But can you give me a (simple) link to start to learn regular expressions? I've looked around and end up scratching my head at it.

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Using PHP to create links on a page

    the best website I've found for beginning regular expressions, and one that can also be used as a reference, is here.

  6. #6
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Using PHP to create links on a page

    Thanks dude .

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Using PHP to create links on a page

    Hi, I'm trying to figure out how I can change

    Code:
    $file = file_get_contents("http://www.vbforums.com");
    So that it actually loads itself rather than a separate different file.

    Any ideas?

  8. #8
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Using PHP to create links on a page

    That would cause an infinite loop (the file would try to get its contents, which would try to get its contents, which would... etc.). Or have I misunderstood you? What are you trying to achieve?

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Using PHP to create links on a page

    I see what you mean -

    What I've done now is split my page into 2 sections:

    about.php
    and
    sidebarlinks.php

    And it sidebarlinks.php which has the following code above as you've advised, with:

    Code:
    $file = file_get_contents("about.php");
    I'm wondering, does that still cause an infinite loop since sidebarlinks.php forms part of about.php?

    What I would like is for sidebarlinks.php (or some code within about.php) to just search the html part of the page for the anchor name tags and then output the a href tags.

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Using PHP to create links on a page

    first of all, you should get things straight. calling file_get_contents() on a LOCAL file will open that file as plain text. you will not be able to parse anchor tags from this file unless they're all static. if you are only opening this file (and not running this file), then everything changes. you can open that file and parse it like I did in my original post. if you wish to open a file after it has been run, you need to provide an http path: http://example.com/about.php.

    have you tried what you're wanting to do and failed? or.. what?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width