hi
i'm kinda new to php, though i have done some programming before. what i'm trying to do, is to replace all instances of <img src="xxx"> with <img src="$address/xxx">, where xxx is the filename, and $address is the variable holding the address of the website.
can anyone help?
str_replace
Moderator: General Moderators
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
str_replace() will only replace exact matches to the string you supply ($search) with the replacement argument ($replace) on the $subject.
If the string needs to be more flexible, you may need to use regular expressions (preg_replace()).
If the string needs to be more flexible, you may need to use regular expressions (preg_replace()).
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
The code i'm currently using is:
edit: And nothing happens! Can someone please give me some sample code?
Code: Select all
<?php
if ($_POST['submit']) {
$address = $_POST['address'];
if (!$contents = readfile($address)) {
echo "<b>Error: Cannot read file! Either file is empty or file does not exist.</b>";
exit;
}
$searchstring = "src=\"";
$replacestring = "src=\"$address";
$contents = str_replace($searchstring, $replacestring, $contents);
echo $contents;
}
?>
Last edited by phoenix121 on Sun Sep 25, 2005 9:24 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
readfile() reads the file and outputs it directly to the output stream, you may want to use file_get_contents()
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
-
phoenix121
- Forum Commoner
- Posts: 28
- Joined: Sun Sep 25, 2005 9:09 pm
- Location: New Zealand
actually, i'm just experimenting with php. it would also be great help if you could help me with my other problem ->
viewtopic.php?t=38631
viewtopic.php?t=38631