2012-08-13 58 views
0

你好,我試圖讓PHP返回一個文件名,並返回字符串作爲將被一個JavaScript插件使用的rel標籤的一部分。php文件名達到特定字符

我需要添加一些PHP腳本返回的文件名字符串到第二hyphon: 到目前爲止,我已經

<?php 
$filename = basename (get_attached_file(get_post_thumbnail_id())); 
?> 
<?php echo pathinfo($filename, PATHINFO_FILENAME); ?> 

返回

tulip-367-011文件名是tulip-367-011.jpg我怎麼得到它返回tulip-367

您好我試過

<?php 
$filename = basename (get_attached_file(get_post_thumbnail_id())); 
?> 
<?php echo $file_parts = explode('-', basename($filename)); 
array_pop($file_parts); // Remove the last segment 
$split_name = implode('-', $file_parts); 
?> 

它返回數組 - 不是一個時間!那真的會讓我輕笑,我已經在這個星期呆過了一個星期了。再次感謝幫助,我會繼續努力。我不是一個很好的程序員,所以請隨時嘲笑我的愚蠢。阿薩

我試着

$file_parts = explode('-', basename($filename)); 
array_pop($file_parts); // Remove the last segment 
$split_name = implode('-', $file_parts); 

而且它也表示數組。再次感謝你的幫助。

回答

1

只需使用basename而不是fileinfo

例子:

$file_without_extension = basename($filename) 

編輯

對不起,我看錯了原來的問題,假設文件名是一致的,除去最後一段做這樣的事情:

$file_parts = explode('-', basename($filename)); 
array_pop($file_parts); // Remove the last segment 
$split_name = implode('-', $file_parts; 
+0

如果您還需要從文件中刪除幾個字符http://php.net/manual/en/function.substr.php可能會有所幫助 – Tim 2012-08-13 18:45:48

+0

解決它大,非常感謝。作爲一個 – 2012-08-14 09:40:18

0
$fileParts = pathinfo(get_attached_file(get_post_thumbnail_id())); 
$nameParts = explode('-', $fileParts['basename']); 
if (count($nameParts) > 1) { 
    $myName = $nameParts[0]+'-'+$nameParts[1]; 
} else { 
    $myName = $nameParts[0]; 
} 
0

固定它:

<?php 
$filename = basename (get_attached_file(get_post_thumbnail_id())); 
$file_parts = explode('-', basename($filename)); 
array_pop($file_parts); // Remove the last segment 
$split_name = implode('-', $file_parts); 
echo $split_name; 
?> 

<a itemprop="image" href="<?php echo wp_get_attachment_url(get_post_thumbnail_id()); ?>" class="Magic360" rel="filename:<?php echo $split_name?>-{col}.jpg; magnify:true; magnify-filename:<?php echo get_post_meta($post->ID, 'magnified_image_filename', true); ?>-{col}.jpg"><img src="<?php echo get_post_meta($post->ID, 'magnified_image', true); ?>" 
相關問題