|
-
May 4th, 2010, 02:05 AM
#1
Thread Starter
Hyperactive Member
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).
-
May 4th, 2010, 02:51 AM
#2
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.
-
May 4th, 2010, 05:01 PM
#3
Thread Starter
Hyperactive Member
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.
-
May 6th, 2010, 02:48 AM
#4
Fanatic Member
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.
-
May 6th, 2010, 10:30 AM
#5
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.
-
May 9th, 2010, 12:33 AM
#6
Fanatic Member
Re: Using PHP to create links on a page
Thanks dude .
-
May 18th, 2010, 04:05 PM
#7
Thread Starter
Hyperactive Member
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?
-
May 18th, 2010, 04:50 PM
#8
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?
-
May 18th, 2010, 05:00 PM
#9
Thread Starter
Hyperactive Member
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.
-
May 18th, 2010, 05:54 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|