[英]Using PHP Sub String to Link Thumbnails
因此,我正在制作一個簡單的庫,用於從目錄中獲取圖像。 所有圖像的名稱都類似於tree_th.jpg,我想使用子字符串(我認為這是正確的)來切出_th並鏈接到僅tree.jpg
<?
$imagetypes = array("image/jpeg", "image/gif");
function getImages($dir) {
global $imagetypes;
$dir = "img/";
$retval = array();
if(substr($dir, -1) != "/") $dir .= "/";
$fulldir = "/$dir";
//echo $fulldir;
$d = @dir($fulldir) or die("");
while(false !== ($entry = $d->read())) {
if($entry[0] == ".") continue;
$f = escapeshellarg("$fulldir$entry");
$mimetype = trim(`file -bi $f`);
foreach($imagetypes as $valid_type) {
if(preg_match("@^{$valid_type}@", $mimetype)) {
$retval[] = array( 'file' => "$dir$entry", 'size' => getimagesize("$fulldir$entry") );
break;
}
}
}
$d->close(); return $retval;
}
$thumbs = getImages("img");
foreach($thumbs as $img) {
echo "<img class=\"photo\" src=\"{$img['file']}\" {$img['size'][1]} alt=\"\">\n";
}
?>
一種實現方法是使用PHP中的str_replace()函數。
$large_image_path = str_replace("_th","",$thumb_path);
您必須首先使用substring命令評估_th的位置,然后使用長度和concat重新組裝字符串,所以我認為str_replace對您而言會更容易。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.