parsing html links with php [100 credits]

Status
Not open for further replies.

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
i have a html page for ex:

HTML:
..........<body>
<a href="jj5f.txt">rtt</a><br>
<a href="jjf75.txt">rtt</a><br>
<a href="jjof.txt">rtt</a><br>

<a href="jj8f.txt">rtt</a><br>
</body>............

so ill then open that page
PHP:
$content=fopen('page.html');

from $content i want to find out the links ending only with .txt and echo them as
PHP:
<a href="mydomain.com/folder/jj8f.txt">rtt</a>
ill pay 100 credits for this
 

VPmase

New Member
Messages
914
Reaction score
0
Points
0
1) Why would you fopen an HTML page?
2) Why don't you just add the links to the php page manually?
 

sadm1r

New Member
Messages
9
Reaction score
0
Points
0
hi there.
i didn't quit understood what you aimed at, and am in kinda hurry ( my pizza is cooling down :D).
if i got it well, you have one file named e.g. "site.html" and you want to take text links from that file and have it displayed on your other web page.
i've just made some small code, it might need some tweaks because it can be possibly buggy.
so let me explain what it does.
it opens your file named 'site.html'.
then it takes line by line and check it... if line contains parts of string (all 3 required in what i made) "a href" && ".txt" && "</a>", it displays the line.
Code:
<?php
echo "links ending with .txt extension:<br>";
$file = fopen('site.html',"r");
$str = array("a href", ".txt", "</a>");
while(!feof($file))
{
	$line = fgets($file);
	$i=0;
	do{
		$check = strpos($line,$str[$i]);
		if($i==2 && $check)
		{
			echo $line;
		}
		$i++;
	}while($check && $i<3);
}
fclose($file);
?>
this might have some problems if your anchor passes into the next line. or you-name-what situation that can occur (e.g. link that contains .txt as part of it, but isnt text file etc).
but i guess it should work well for what you asked :))))
gl i hope it helped d(^_^)b
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
please explain what you need better. we cant do somthing for you if you dont help us understand
 

sadm1r

New Member
Messages
9
Reaction score
0
Points
0
please explain what you need better. we cant do somthing for you if you dont help us understand

i think his situation is:
he has one page where his links are posted (or where he adds the links manually). he wants certain data (links to text files) to be visible from the other web page as well.

my guess is that he wants txt files posted on other pages as well once he adds them on other site, and he wants to solve it with by some code so that he doesn't have to alter several pages once he adds up some info somewhere on one page.

e.g. like when you make a database, and want to use reference key so that you don't have to alter data each time in several tables.
 
Last edited:

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
iam opening a remote file using curl to do this and hence i cant use fgets
i tried some html parsers but they only parse link such as "<a href="http://xyz.com/www.txt">grg</a> but not "<a href="xyz.txt">grg</a>"
here is the code iam using

PHP:
<?

if (function_exists('curl_init')) 
{
  
   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL, 'http://xyz.xom/site.html');

   
   curl_setopt($ch, CURLOPT_HEADER, 0);

  
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');

   $content = curl_exec($ch);
//echo"$content";

   curl_close($ch);
} else {
 echo" error while connecting";
}
?>
 
Status
Not open for further replies.
Top