【问题标题】:Check substring in string array in PHP在 PHP 中检查字符串数组中的子字符串
【发布时间】:2014-10-02 14:44:59
【问题描述】:

我是 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>';
}
?>

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

{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}

... 如您所见,我想检查文件名字符串数组中的字符串名称。如果我有更多类别,如苹果或香蕉,名称总是以数字 1、2、3 等结尾。 用php怎么做?

【问题讨论】:

  • 使用basename函数获取路径的文件名部分,去掉.jpg后缀,explode()获取空格前的部分。
  • 你不明白我想做什么。你可以看看我已经完成的那部分。我想检查作为文件名的字符串数组中的字符串。得到我描述的输出。
  • 那你的问题还不够清楚。在我看来,您想从images/fruits/apple 1.jpg 获取apple,并在滑块标题中显示。
  • 实用。我想检查字符串的最后一个字符,如果该字符是数字并且字符串中的其余字符与数组中的其他一些字符串相同,该字符串包含数字中的最后一个字符,则执行此操作,否则执行其他操作...简而言之
  • 为什么只有字符串的最后一个字符?如果是apple 10.jpg 怎么办?

标签: php html string compare output


【解决方案1】:

您可以尝试 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 所以“苹果绿 1.jpg” -> (\w+|\w+\s\w+)\s\d.\w+

【讨论】:

  • 我还有一个小问题。不要打开另一个问题。你知道路径和图像的编码是如何运作的吗?我来自塞尔维亚,所以如果我的图片被命名为 kruška.jpg(kruška 在翻译中是梨)不起作用,我会为滑块名称 kruÅ¡ka 得到这个,并且图像的路径是 image/fruits/kruÅ¡ka.jpg 并且没有加载?有没有解决办法。还必须为其他塞尔维亚字符工作,如 Č,č,Ć,ć,Ž,ž
  • 这很奇怪!如果您想安全,请使用 7 位 ASCII。阅读有关 urlencode() 和 utf8_encode() 的信息。为页面设置 utf8 标头。更多:stackoverflow.com/a/1556989/1163786
  • 我现在看,在导出 txt 文件中一切正常,但在 echo 上却没有 :) 很奇怪。哦,车轮之谜。对我来说它的重要导出文件。
【解决方案2】:

如果我知道您想以滑块提供的格式对图像进行分组,这实际上很简单,只需从文件名中提取滑块标题,然后将其作为其数组的键来对图像进行分组:

<?
// 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}

【讨论】:

  • 是的,就是这样。抱歉,我写的问题是我是 PHP 的新手,所以我不知道 PHP 中的函数和声明以及那些东西到底是什么,但我非常了解 OOP。谢谢,你也很有帮助,我也想用这个代码作为备份。
  • @MorsViolenta 我已经编辑了我的答案。使用$files 数组下方的文件迭代器匹配所有图像文件,无论其文件名如何(支持塞尔维亚字符)
  • 你是国王! :) 非常感谢。
  • 它不工作 :( "解析错误:语法错误,第 20 行 scriptgal.php 中的意外 '['"
  • 哦,您使用的是旧版本的php。把它分成两行。 $pathinfo = pathinfo($file);$files[] = $pathinfo['basename'];
【解决方案3】:

使用正则表达式获取文件名中空格和数字之前的部分,并将其与之前保存的单词进行比较。如果不同,则显示 {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>';
}

【讨论】:

  • 如果我有类似图片的东西,叫做 apple green 1.jpg apple green 2.jpg,字符串中有 2 个空格?
  • 我已将其更改为使用正则表达式来获取数字之前的文件名部分。
  • 谢谢,你很有帮助!这就是我想要的。
  • 呵呵,我之前发过这个:)
  • @Jens-AndréKoch 我在你发布的同时进行了更改
猜你喜欢
  • 1970-01-01
  • 2020-11-28
  • 2020-07-06
  • 2011-02-18
  • 2020-11-21
  • 1970-01-01
  • 2021-12-14
  • 2012-02-15
  • 2019-01-09
相关资源
最近更新 更多