Forum Moderators: coopster

Message Too Old, No Replies

Parse HTML <A HREF Tags in PHP. Immediate Assistance Please

Parse HTML <A HREF Tags in PHP

         

Anyango

6:51 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello All.

i am stuck in something which is very very important for one of our ongoing projects, Immediate Assistance would be very highly appreciated as i am running out of time.

PROBLEM :

Lets say i have some html code in a variable right

$data=
"
<html><head><title>Parsing</title></head>
<body>
<a href='http://www.thisislink1.com'>Link1</a><br>
<a href='http://www.thisislink2.com'>Link2</a><br>
<a href='http://www.thisislink3.com'>Link3</a><br>
</body>
</html>
";

Now you see that variable has some html code with 3 hyperlinks in it. i just want to change those links in that html in that variable without changing anything else. i mean a function which could allow me to change those links by like parsing all that html and putting all 3 of them in seperate array indexes and then i could change those array index values and could rejoin all that to create the same html as before but with links updated..

DESIRED SAMPLE OUTPUT:

for this variable

$data=
"
<html><head><title>Parsing</title></head>
<body>
<a href='http://www.thisislink1.com'>Link1</a><br>
<a href='http://www.thisislink2.com'>Link2</a><br>
<a href='http://www.thisislink3.com'>Link3</a><br>
</body>
</html>
";

i would say change link1 to this, link2 to this, link3 to this. and output would be like

$data=
"
<html><head><title>Parsing</title></head>
<body>
<a href='http://www.newlink1.com'>Link1</a><br>
<a href='http://www.newlink2.com'>Link2</a><br>
<a href='http://www.newlink3.com'>Link3</a><br>
</body>
</html>
";

I think preg_replace or parsing of html tags would solve this problem but my tries are not working.

I hope my problem description is clear..

I thank you all for your time..

Please My whole project depends on this function, PLease help me if you have some time to share your knowledge.

Thank You very much

Kamran

Anyango

7:32 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and also Please note that the source html data in my variable will always be different so ofcourse links will be different. so we cannot just assume that it would be "http://www.thisislink1.com" and cannot just simply use

$data=str_replace("http://www.thisislink1.com","http://newlink1.com",$data)

Our Source link values will always vary which we would not be knowing beforehand

tomda

7:36 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure, but can you not just do

$data= "<html><head><title>Parsing</title></head><body><a href='".$link1."'>Link1</a><br><a href='".$link2."'>Link2</a><br><a href='".$link1."'>Link3</a><br></body></html>";

and have an include file where
$link1='your_link1';
$link2='your_link2';
$link3='your_link3';

and make as many include file as you want

Anyango

8:06 pm on Jun 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes but value of that variable $data comes from outside my control, i mean i cannot put variables in it. i can just use it and then edit it as wanted by cannot actually

put variables in the source ..

Actually its like each time my code is called, i am importing HTML of any webpage from the web so you see thats not in my control that what html i 'll get.

Please Comment