2

I am a little be a newbie in PHP (I have much experience in other programming languages like C# etc). So from experience in OOP, i am almost sure that this can be done also in PHP. Here is the question and problem. I am writting script thats going to export some html code. Here is example. I have folder on server with images of fruits. For example folder holds next images: strawberry.jpg, apple 1.jpg, apple 2.jpg, peach.jpg, bannana 1.jpg, bannana 2.jpg, pear.jpg. I am already written code thats working perfect but I want to be more advanced

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

So what I want. As you see my output will be

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

...

And etc for each image. I want next i want to my output for each this slider that apple be in that slide for example. I want to my output be like this:

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

... So as you see I want to check names which are strings in string array of filenames. If i have more categories like apples, or bannanas, the names always end with number 1,2,3 etc. How can be done in php?

Mors Violenta
  • 129
  • 2
  • 13
  • Use the `basename` function to get the filename part of the path and remove the `.jpg` suffix, and `explode()` to get the part before the space. – Barmar Oct 02 '14 at 14:50
  • You dont understanded what I want to do. You can se that part I already done. I want to check strings in array of strings that are filenames. To get output as I described. – Mors Violenta Oct 02 '14 at 14:51
  • Then your question isn't clear enough. It looks to me like you want to get `apple` from `images/fruits/apple 1.jpg`, and show that in the slider heading. – Barmar Oct 02 '14 at 14:52
  • Practiuclary. I want to check last char of string if the char is number and rest chars in string are the same as some other string in array that holds last char in number do that and that else do something else...In short – Mors Violenta Oct 02 '14 at 14:54
  • Why just the last char of the string? What if it's `apple 10.jpg`? – Barmar Oct 02 '14 at 14:55
  • Use `explode` to split the string at the space. Then check if the first part is the same as the previous one, which you save in a variable. – Barmar Oct 02 '14 at 14:56
  • I dont have, thats just some proposition. Anyhow just want answer if can be done or not. But i am almost sure that can be done! And how can be done. But what if i have something like pic thats called apple green 1.jpg apple green 2.jpg? – Mors Violenta Oct 02 '14 at 15:01

3 Answers3

2

You might try to preg_match() the filename to grab the category. You can modify the regular expression to fit your desired pattern.

$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>';  

}

And i doubt that you can adjust the regexp :D So "apple green 1.jpg" -> (\w+|\w+\s\w+)\s\d.\w+

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • I have one little question more. Not to open another question. Do you know how encoding of path and images functioning. I am from Serbia so if my picture is caled kruška.jpg (kruška is pear in translate) isnt working i get this for slider name kruÅ¡ka, and the path to image is image/fruits/kruÅ¡ka.jpg and isnt loaded? Is there solution. Also must working for other Serbian characters like Č,č,Ć,ć,Ž,ž – Mors Violenta Oct 02 '14 at 16:50
  • That's odd! If you want to be safe, go with 7-bit ASCII. Read about urlencode() and utf8_encode(). Set utf8 header for the page. More: http://stackoverflow.com/a/1556989/1163786 – Jens A. Koch Oct 02 '14 at 19:20
  • I looked now, in export txt file everything is ok, but on echo isnt :) Very odd. Oh wheel mystery. To me its important export file. – Mors Violenta Oct 02 '14 at 20:14
2

If I understand you want to group your images in the format provide for your slider, that is actually simple, just extract the slider header from the file name and then put it as the key of its array to group the images:

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

That would print (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}
Axel A. García
  • 683
  • 9
  • 21
1

Use a regular expression to get the part of the filename before the space and number, and compare this to the word saved previously. If it's different, display the {slider ...} header.

$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>';
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What if i have something like pic thats called apple green 1.jpg apple green 2.jpg, Thats 2 spaces in string? – Mors Violenta Oct 02 '14 at 15:14
  • 1
    I've changed it to use a regexp to get the part of the filename before the number. – Barmar Oct 02 '14 at 15:20
  • Thanks, you are been very helpful! Thats what I wanted. – Mors Violenta Oct 02 '14 at 15:23
  • 1
    hehe, i posted this before :) – Jens A. Koch Oct 02 '14 at 15:23
  • 1
    @Jens-AndréKoch I was making my change at the same time as you posted yours – Barmar Oct 02 '14 at 15:25
  • I have one little question more. Not to open another question. Do you know how encoding of path and images functioning. I am from Serbia so if my picture is caled kruška.jpg (kruška is pear in translate) isnt working i get this for slider name kruÅ¡ka, and the path to image is image/fruits/kruÅ¡ka.jpg and isnt loaded? Is there solution. Also must working for other Serbian characters like Č,č,Ć,ć,Ž,ž – Mors Violenta Oct 02 '14 at 16:38
  • I don't know, but maybe this question will be helpful: http://stackoverflow.com/questions/18204364/php-upload-utf-8-filename – Barmar Oct 02 '14 at 19:27