Forum Moderators: coopster
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
$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
$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
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