[英]How to remove beginning of filename in PHP?
I'm making a gallery in jssor and the slides are made by reading a directory and writing the div for every image using a PHP script. 我正在用jssor制作图库,并通过读取目录并使用PHP脚本为每个图像编写div来制作幻灯片。 Under the image a caption is displayed with the name of the artist (this is always the same) followed by the file name without the extension.
在图像下方,显示带字幕的标题,其中包含艺术家的姓名(始终相同),后跟不带扩展名的文件名。
This is what I have so far: 这是我到目前为止的内容:
<?
$dir = 'Photo/Paintings';
$files = scandir($dir);
sort($files);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
echo '<div>
<img u="image" style="max-height:460px;" src="Photo/Paintings/'.$file.'" />
<img u="thumb" src="Photo/Paintings/'.$file.'" />';
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
echo '<div u="caption" style="position: absolute; top: 470px; left: 0px; height: 10px; text-align: center;">
ARTISTX - '.$withoutExt.'
</div>
</div>';
}
}
?>
These images should be in a particular order, so my idea was to add a number at the beginning of every filename like so #5-Picture of tree.jpg
. 这些图像应按特定顺序排列,所以我的想法是在每个文件名的开头添加一个数字,例如
#5-Picture of tree.jpg
。 My question is, how do I remove this #5
part from being displayed in the caption as well? 我的问题是,如何将这
#5
部分也从标题中删除?
Alternatively, is there a better way to determine the order of these files? 或者,是否有更好的方法来确定这些文件的顺序? I realize now my idea wouldn't even work very well since #1 would alphabetically be followed by #11 and #12 instead of #2.
我意识到我的想法甚至无法很好地执行,因为#1按字母顺序紧随其后的是#11和#12,而不是#2。 I could maybe work around this by using a combination with letters 1A, 1B, 1C, 2A, etc.
我也许可以通过结合使用字母1A,1B,1C,2A等来解决此问题。
You can get only a part of the string: 您只能获得字符串的一部分:
$str = substr($str, 3);
Or you delete the part before the -
explicit: 或者,您删除
-
显式前的零件:
$ex = explode("-", $str);
unset($ex[0]);
$str = implode("-", $ex);
Second method is better if the first part can have more than 3 characters. 如果第一部分可以包含3个以上的字符,则第二种方法更好。
Alternatively, is there a better way to determine the order of these files?
或者,是否有更好的方法来确定这些文件的顺序? I realize now my idea wouldn't even work very well since #1 would alphabetically be followed by #11 and #12 instead of #2.
我意识到我的想法甚至无法很好地执行,因为#1按字母顺序紧随其后的是#11和#12,而不是#2。 I could maybe work around this by using a combination with letters 1A, 1B, 1C, 2A, etc.
我也许可以通过结合使用字母1A,1B,1C,2A等来解决此问题。
Use this: 用这个:
$myFiles = Array();
foreach($files as $file){
// ...
$myFiles[str_replace("#", "", explode("-", $file)[0]))] = $file;
}
ksort($myFiles);
foreach($myFiles as $file){
// display
}
This only take effect if the file name actually starts with a number: 仅当文件名实际上以数字开头时,此命令才生效:
$str = preg_replace('/^\\d+-/', null, $fileName);
Files which do not start with #{number}-
will not be affected by this tailoring process. 不以
#{number}-
开头的文件将不受此定制过程的影响。
For your sorting problem. 对于您的排序问题。 Put a filling
0
before. 在前面放一个填充
0
。 So use 01
, 02
, ... and so on up to 99
. 因此,使用
01
, 02
,...等多达99
。
By the way. 顺便说说。 Don't use short PHP tags.
不要使用简短的PHP标记。 Always use
<?php
at the beginning of a PHP block. 始终在PHP块的开头使用
<?php
。
要回答第二个问题,您不必更改为1A,2B ...有点像natsort natsort
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.