簡體   English   中英

在PHP中檢查字符串數組中的子字符串

[英]Check substring in string array in PHP

我是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中完成?

您可以嘗試使用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”-> (\\w+|\\w+\\s\\w+)\\s\\d.\\w+

如果我了解您要按照為滑塊提供的格式對圖像進行分組,那實際上很簡單,只需從文件名中提取滑塊標題,然后將其作為數組的鍵來對圖像進行分組:

<?
// 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示例 ):

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

使用正則表達式獲取文件名中空格和數字之前的部分,並將其與先前保存的單詞進行比較。 如果不同,則顯示{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>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM