在PHP中检查字符串数组中的子字符串

时间:2014-10-02 14:44:59

标签: php html string compare output

我有点像PHP的新手(我在C#等其他编程语言方面有很多经验)。所以根据OOP的经验,我几乎可以肯定这也可以在PHP中完成。这是问题和问题。我正在写脚本,将导出一些HTML代码。这是一个例子。我在服务器上有水果图像的文件夹。例如文件夹保存下一个图像:strawberry.jpg,apple 1.jpg,apple 2.jpg,peach.jpg,bannana 1.jpg,bannana 2.jpg,pear.jpg。我已经编写了完美的代码,但我想要更高级



<?php
$files = glob('./images/fruits/*.*');
foreach($files as $file) {
    $filename = pathinfo($file);
	$filepath1 = pathinfo($file, PATHINFO_BASENAME);
	$filepath2 = "images/logotipi/fruits/".$filepath1;
    echo '{slider <strong>'.$filename['filename'].'</strong>|blue}';
	echo '<br>';
	echo '{tip';
	echo "<img src=\"".$filepath2."\">";
	echo '}';
	echo "<img src=\"".$filepath2."\" ";
	echo 'width="100" height="100" />{/tip}';
	echo '<br>';
}
?>
&#13;
&#13;
&#13;

所以我想要的。如您所见,我的输出将是

{slider **strawberry**|blue}
{tip <img src="images/fruits/strawberry.jpg" />}<img src="images/fruits/strawberry.jpg" width="100" height="100" />{/tip}
{slider **apple 1**|blue}
{tip <img src="images/fruits/apple 1.jpg" />}<img src="images/fruits/apple 1.jpg" width="100" height="100" />{/tip}
{slider **apple 2**|blue}
{tip <img src="images/fruits/apple 2.jpg" />}<img src="images/fruits/apple 2.jpg" width="100" height="100" />{/tip}

...

每张图片等等。我想接下来我希望我的输出为每个这个滑块,例如苹果在那张幻灯片中。我想我的输出是这样的:

 {slider **strawberry**|blue}
    {tip <img src="images/fruits/strawberry.jpg" />}<img src="images/fruits/strawberry.jpg" width="100" height="100" />{/tip}
    {slider **apple**|blue}
        {tip <img src="images/fruits/apple 1.jpg" />}<img src="images/fruits/apple 1.jpg" width="100" height="100" />{/tip}
        {tip <img src="images/fruits/apple 2.jpg" />}<img src="images/fruits/apple 2.jpg" width="100" height="100" />{/tip}

... 因此,如您所见,我想检查文件名字符串数组中的字符串名称。如果我有更多的类别,如苹果或bannanas,名称总是以数字1,2,3等结尾。 如何在PHP中完成?

3 个答案:

答案 0 :(得分:2)

您可以尝试使用preg_match()文件名来获取类别。 您可以修改正则表达式以适合您想要的模式。

$lastCategory = null;

foreach($files as $file) {

    // adjust this regular expression pattern to fit your needs
    preg_match("/(\w+)\s\d.\w+/", $file, $matches);
    $filename = $matches[0];
    $category = $matches[1];

    // do we have a new category?
    if ($category !== $lastCategory) {
        echo '{slider <strong>' . $category . '</strong>|blue} <br>';
        $lastCategory = $category;
    }

    echo '{tip <img src="' . $file . '"> }';
    echo '<img src="' . $file . '" width="100" height="100" />';
    echo '{/tip} <br>';  

}

我怀疑你可以调整正则表达式:D 所以“apple green 1.jpg” - &gt; (\w+|\w+\s\w+)\s\d.\w+

答案 1 :(得分:2)

如果我了解你想要以滑块的格式对图像进行分组,那实际上很简单,只需从文件名中提取滑块标题,然后将其作为其数组的键来对图像进行分组: / p>

<?
// Serialize data ------------------------------------>
function clearHeader($filename) {
    // Remove extension
    // $rf = pathinfo($file, PATHINFO_BASENAME); # This will not work in my example as I don't have the images
    $rf = explode('.', $filename);
    array_pop($rf);
    $hd = $rf[0];
    // Remove numbers
    $hd = preg_replace('/\s?\d+/', '', $hd);
    // Replace spaces for underscores
    $hd = str_replace(' ', '_', $hd);
    // Return the header name
    return $hd;
}

$files = array('strawberry.jpg','apple 1.jpg','apple 2.jpg','peach.jpg','bannana 1.jpg','bannana 2.jpg','pear.jpg');
/*
// This file iterator is better than glob()
$scan = new RecursiveDirectoryIterator(__DIR__ . '/images/fruits/');
foreach(new RecursiveIteratorIterator($scan) as $file) {
    if(@!is_array(getimagesize($file))){
        $files[] = pathinfo($file)['basename'];
    }
}
*/
foreach ($files as $filename) {
    $slider[ clearHeader($filename) ][] = $filename;
}

// Display data -------------------------------------->
foreach ($slider as $header => $files) {
    $slider_line = array();
    $slider_line[] = "{slider **{$header}**|blue}";
    foreach ($files as $file) {
        $slider_line[] = "{tip <img src=\"images/fruits/{$file}\" />}<img src=\"images/fruits/{$file}\" width=\"100\" height=\"100\" />{/tip}";
    }
    echo implode(PHP_EOL, $slider_line) . PHP_EOL;
}

那将打印(Codepad example):

{slider **strawberry**|blue}
{tip <img src="images/fruits/strawberry.jpg" />}<img src="images/fruits/strawberry.jpg" width="100" height="100" />{/tip}
{slider **apple**|blue}
{tip <img src="images/fruits/apple 1.jpg" />}<img src="images/fruits/apple 1.jpg" width="100" height="100" />{/tip}
{tip <img src="images/fruits/apple 2.jpg" />}<img src="images/fruits/apple 2.jpg" width="100" height="100" />{/tip}
{slider **peach**|blue}
{tip <img src="images/fruits/peach.jpg" />}<img src="images/fruits/peach.jpg" width="100" height="100" />{/tip}
{slider **bannana**|blue}
{tip <img src="images/fruits/bannana 1.jpg" />}<img src="images/fruits/bannana 1.jpg" width="100" height="100" />{/tip}
{tip <img src="images/fruits/bannana 2.jpg" />}<img src="images/fruits/bannana 2.jpg" width="100" height="100" />{/tip}
{slider **pear**|blue}
{tip <img src="images/fruits/pear.jpg" />}<img src="images/fruits/pear.jpg" width="100" height="100" />{/tip}

答案 2 :(得分:1)

使用正则表达式在空格和数字之前获取文件名的一部分,并将其与之前保存的单词进行比较。如果不同,请显示{slider ...}标题。

$lastfruit = null;
foreach($files as $file) {
    $filename = pathinfo($file);
    $filepath1 = pathinfo($file, PATHINFO_BASENAME);
    $filepath2 = "images/logotipi/fruits/".$filepath1;
    preg_match('#/([^/]+?)(?: \d+)?\.[^/]+$#', $file, $match);
    $fruit = $match[1];
    if ($fruit != $lastfruit) {
        echo '{slider <strong>'.$fruit.'</strong>|blue}';
        echo '<br>';
        $lastfruit = $fruit;
    }
    echo '{tip';
    echo "<img src=\"".$filepath2."\">";
    echo '}';
    echo "<img src=\"".$filepath2."\" ";
    echo 'width="100" height="100" />{/tip}';
    echo '<br>';
}