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?

Recommended Answers

All 4 Replies

Have a look at str_ireplace and preg_replace. It's all in the manual!

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 . "<br />";
    }
    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?

Maybe you could try to use single-quotes on the outside. You can't have double-double quotes.

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 . "<br />";
    }

}

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.