如果我正確理解你,你有一個source_file.htm,如下所示。現在
<!DOCTYPE html>
<html>
<body>
<img src="myprettypicture.jpg" alt="myprettypicture.jpg" height="50" width="50">
<img src="mynotsoprettypicture.jpg" alt="mynotsoprettypicture.jpg" height="50" width="50">
</body>
</html>
,這是更好地爲您的圖片的alt標籤的替代存儲在一個文件images.ini
那樣。
myprettypicture.jpg = "Here's a pretty picture"
mynotsoprettypicture.jpg = "Here's a not so pretty picture"
現在你可以運行這個PHP腳本:
<?php
$ini_array = parse_ini_file("images.ini"); // load "images.ini" into array
foreach($ini_array as $key => $val)
{
$img[] = 'alt="' . $key . '"';
$alt[] = 'alt="' . $val . '"';
}
$source = file_get_contents('source_file.htm'); // get Your "source_file.htm"
$target = str_replace($img, $alt, $source); // make all required replacements
file_put_contents('target_file.htm', $target); // save the result to "target_file.htm"
?>
得到target_file.htm
<!DOCTYPE html>
<html>
<body>
<img src="myprettypicture.jpg" alt="Here's a pretty picture" height="50" width="50">
<img src="mynotsoprettypicture.jpg" alt="Here's a not so pretty picture" height="50" width="50">
</body>
</html>
參考: str_replace,parse_ini_file,file_get_contents,file_put_contents
這正是我被搜索興奮:某種圖像文件夾的.ini包裝。非常感謝您的評論,非常有用。 – mediaklan