Hi everyone,
I couldn`t figure out the pattern which is used with the eregi_replace function.this is my case,
I have a php script which will read files(html,php...etc) and get all the contents of that file into a variable. then if that file contents has <?php tag (if that file has php scripts) i take that whole php part and write it to a txt file and replace all that php script with an image. there isn't any problem with this part. Please find below the script that I wrote:
@$fileContents="";
if(@$fp=fopen(@$file,"r+"))
{
while (!feof($fp))
{
@$fileContents .= fread ($fp,1024);
}
fclose ($fp);
}
else
{
print "error opening file";
die();
}
@$middleContents=@$fileContents;
$wholeParts=explode("<?",@$middleContents);
$first=trim(@$wholeParts[0]);
$seAndLast=@$wholeParts[1];
$secondWhole=explode("?>",@$seAndLast);
$middle=trim(@$secondWhole[0]);
//write the php script to the temp file if(@$fp=fopen("temp/aa.txt","w"))
{
fwrite($fp,"<?$middle?>");
fclose($fp);
}
else
{
print "error opening file";
die();
}
//here i replace all the urls to complete urls (eg: /images/aa.jpg to htttp://whatever.com/images/aa.jpg"
$first = eregi_replace("src=(\"|')*(\"|')|src=|background=(\"|')*(\"|')|background=|href=(\"|')*(\"|')|href=", "\\0$URL\\1",$first);
$last = eregi_replace("src=(\"|')*(\"|')|src=|background=(\"|')*(\"|')|background=|href=(\"|')*(\"|')|href=", "\\0$URL\\1",trim(@$secondWhole[1]));
//Final string with the replaced img
$finalBody=$first."<img src=\"".$phpIcon."php.jpg\" id=\"img1\">".$last;
after doing changes to that string i write back the whole string back to the file. but when i`m writting it i replace that image with the old php script. This is what i wrote,
$middleHtmlPart=stripslashes($txtPageBody);
$str=trim($middleHtmlPart);
if(file_exists("temp/aa.txt"))
{
@$fileContents="";
if(@$fp=fopen("temp/aa.txt","r+"))
{
while (!feof($fp))
{
@$fileContents .= fread ($fp,1024);
}
fclose ($fp);
}
else
{
print "error opening file";
die();
}
unlink("temp/aa.txt");
}
$imgUrl="<img src=\"".$phpIcon."php.jpg\" id=\"img1\">";
//replace img back to php (This didnt work)
$finalStr=eregi_replace($imgUrl, $fileContents, $str);
$finalText =str_replace($URL,"",$finalStr);
if($fp=fopen($file,"w"))
{
fwrite($fp,$finalText);
fclose($fp);
}
Here replacing image back to php is not working. Can someone please help me with my problem..??????😕
thanx in advance,
regards,
Niroshan