How can I edit html links using php?
Let's say I have a huge variable called $file
$file contains thousands of lines of html, css, javascript and more.
But, I want to be able to find all of these link and edit them.
I want to only edit the ones that don't have a domain name.
Example: I would edit this link: /folder/folder/file.css
I would change it to: http://www.domain.com/folder/folder/file.css
Is there a way I can do this with php automatically?
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
Have a look at str_ireplace and preg_replace. It's all in the manual!
chrishea
Nearly a Posting Virtuoso
1,429 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
Thanks, but I don't understand why this won't work correctly??
<?php
session_start();
if(isset($_POST['url']))
{
//Variables
$URL = $_POST['url'];
$open = fopen($URL,"r");
$CSS_Code = "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
$CSS_Final_Code = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$URL";
echo "<textarea cols=\"400\" rows=\"100\" >";
while(! feof($open))
{
$Line = fgets($open);
str_ireplace(stripslashes($CSS_Code),stripslashes($CSS_Final_Code),$Line);
echo $Line . "";
}
echo "</textarea>";
}
?>
None of the code that gets echoed in the text box is changed? It still doesn't include the real url link to the css style sheet?
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
I found out what I did. I set $Line equal to the function. Now I want to be able to do the str_ireplace() more than once? Here's what I know works...
if(isset($_POST['url']))
{
//Variables
$URL = $_POST['url'];
$open = fopen($URL,"r");
$CSS_Code = "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
$CSS_Final_Code = "<link rel=\"stylesheet\" type=\"text/css\" href=\"$URL";
$HTML_Link = "<a href=\"";
$HTML_Final_Link = "<a href=\"$URL";
while(! feof($open))
{
$Line = fgets($open);
$Line = str_ireplace(stripslashes($CSS_Code),stripslashes($CSS_Final_Code),$Line);
echo $Line . "";
}
}
But, I don't have any idea on how I can do it twice or three times. I tryed doing it twice, but the whole page came out twice. Any ideas?
Joe34
Junior Poster in Training
92 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0