Heyo, I'm a little stuck on this array, any help would be appreciated.

Here's the deal, this first takes all images in my emoticons directory and turns smile.gif into two things
$oldval (or "smile.gif")
and
$val (or "smile")

The next part, I'd like to use the commented out echo "':$val:' => '$oldval'),"; instead of hard coding the :triggername: and the image file names. within the loop, but it doesn't work for some reason. Any suggestions? Thanks!
-Sys

<?php
$contents = "hey :smile:";

$path = ($includes . "/emoticons");
$dir = dir($path);
while ($file = $dir->read()) {
if ((($file != ".") |  ($file != "..")) && (!is_dir($file))) {
$filelist[] = $file;
}
}
asort($filelist);
while (list ($key, $val) = each ($filelist)) {
$oldval = $val;
$val = basename($val, ".gif") ;
$val = basename($val, ".png") ;
$val = basename($val, ".jpg") ;
//echo "':$val:' => '$oldval'),";
}

$smileys = array(

':smile:' => 'smile.gif',
':wink:' => 'wink.gif');

foreach($smileys as $smiley=>$image) {
$contents = str_replace($smiley, '<img src="/includes/emoticons/'.$image.'">', $contents); }
?>

    i still don't quite understand what you are ultimately trying to do. you want to grab all files from a dir and echo the filenames with extension and without?

    $path = $includes . '/emoticons/';
    
    $handle = opendir($path);
    while (($file = readdir($handle)) !== false)
    {
    	if ($file != '.' && $file != '..' && is_file($path . $file))
    	{ 
    		$pathinfo = pathinfo($path . $file);
    		if (isset($pathinfo['extension'])) {$ext = $pathinfo['extension'];} else {$ext = '';}
    		$basename = basename($path . $file, '.' . $ext);
    		$filelist[$basename] = $file;
    	} 
    } 
    
    asort($filelist);
    
    foreach ($filelist as $key => $value)
    {
    	echo 'no ext: <b>' . $key . '</b><br>
    	with ext: <b>' . $value . '</b><br><br>';
    }
    

      Originally posted by devinemke
      i still don't quite understand what you are ultimately trying to do. you want to grab all files from a dir and echo the filenames with extension and without?

      My apologies, I should have made that more clear. It's for a content management system, and if the user input 😄 it converts 😄 into an embedded image, so 😄 would be smile.gif, but I'd like to generate the list of available options out of the directory of images. So for example:
      I drop an image of frown.gif into the folder "emoticons". Immediately, someone can use ":frown:", in their post and it converts it into a link to the image on output.

      Right now it works well, but I have to define all of the :faces: and their image files manually.

      Hope that helps! 😃

        Like this, except, it doesn't work. 🙂

        <?php
        $contents = "hey :smile:";
        $smileys = array(
        
        $path = ($includes . "/emoticons");
        $dir = dir($path);
        while ($file = $dir->read()) {
        if ((($file != ".") |  ($file != "..")) && (!is_dir($file))) {
        $filelist[] = $file;
        }
        }
        asort($filelist);
        while (list ($key, $val) = each ($filelist)) {
        $oldval = $val;
        $val = basename($val, ".gif") ;
        $val = basename($val, ".png") ;
        $val = basename($val, ".jpg") ;
        
        echo "':$val:' => '$oldval'),";
        
        }
        
        
        ':lastimage:' => 'nothing.gif');
        
        foreach($smileys as $smiley=>$image) {
        $contents = str_replace($smiley, '<img src="/includes/emoticons/'.$image.'">', $contents); }
        ?>
        

          here's a very simplified example:

          $path = $includes . '/emoticons/';
          $handle = opendir($path);
          while (($file = readdir($handle)) !== false)
          {
          	if ($file != '.' && $file != '..' && is_file($path . $file))
          	{ 
          		$pathinfo = pathinfo($path . $file);
          		$basename = basename($path . $file, '.' . $pathinfo['extension']);
          		$filelist[$basename] = $file;
          	} 
          }	
          asort($filelist);
          
          if (!isset($_POST['submit']))
          {
          	echo '
          	<form action="" method="POST">
          	post message.  you can use the following emoticons:<br>
          	';
          
          foreach ($filelist as $key => $value)
          {
          	echo ':' . $key . ':<br>';
          }
          
          echo '
          <textarea name="message" cols="30" rows="8"></textarea><br>
          <input type="submit" name="submit" value="submit">
          </form>
          ';
          }
          else
          {
          	// clean up the message
          	$message = strip_tags($_POST['message']);
          	$message = stripslashes($message);
          	$message = htmlspecialchars($message);
          	$message = trim($message);
          
          foreach ($filelist as $key => $value)
          {
          	$message = str_replace(':' . $key . ':', '<img src="' . $path . $value . '">', $message);
          }
          
          echo 'your message:<hr>' . nl2br($message);
          }
          

          you may want to think about putting the filelist array into session so you don't have to reparse the img directory every time. if the emoticons are all different sizes than you might want to look at [man]getimagesize[/man] to automagically get the dimensions for the image tags.

            Originally posted by devinemke
            here's a very simplified example:
            may may want to think about putting the filelist array into session so you don't have to reparse the img directory every time. if the emoticons are all different sizes than you might want to look at [man]getimagesize[/man] to automagically get the dimensions for the image tags.

            Hey devinemke, I appreciate the example a great deal, but it's actually complicating what I already have setup. What you see of my code here is actually a very small piece of the actual system, I was trying to keep it short so it wouldn't be too confusing for anyone to read. All the forms and formatting is all done. I'm just really stuck trying to get an array that works with the variables ":$val:" => $oldval from my while loop.

            I just don't think I can put a while inside of the array in any way. At least it seems so. :/

              Originally posted by devinemke

              may may want to think about putting the filelist array into session so you don't have to reparse the img directory every time. if the emoticons are all different sizes than you might want to look at [man]getimagesize[/man] to automagically get the dimensions for the image tags.

              These are also excellent suggestions. Thank you. 🙂

              -Sys

                You're saying you've got a buncha files named
                "smile.png", "frown.png", etc.. For each name you make $val and $oldval and you want to add an array element to $smilies so that

                $smilies[$val]=$oldval;

                ?

                  Originally posted by Weedpacket
                  You're saying you've got a buncha files named
                  "smile.png", "frown.png", etc.. For each name you make $val and $oldval and you want to add an array element to $smilies so that

                  $smilies[$val]=$oldval;

                  ? [/B]

                  That's about the size of it, yup! I'm still working on it. I have this going so far. Keep in mind, I'm relatively still new to PHP so if my code sucks, it's because I'm still learning. 😉

                  $path = ($includes . "/emoticons");
                  
                  $dir = dir($path);
                  while ($file = $dir->read()) {
                  if ((($file != ".") |  ($file != "..")) && (!is_dir($file))) {
                  $filelist[] = $file;
                  }
                  }
                  asort($filelist);
                  
                  while (list ($key, $val) = each ($filelist)) {
                  $oldval = $val;
                  $val = basename($val, ".gif") ;
                  $val = basename($val, ".png") ;
                  $val = basename($val, ".jpg") ;
                  
                  $smileys = array (
                     ":$val:" => "$oldval"
                  );
                  
                  $smileystemp = array_merge_recursive($smileys, $smileystemp);
                  
                  }
                  
                  $smileys = $smileystemp;
                  
                  foreach($smileys as $smiley=>$image) {
                  $contents = str_replace($smiley, '<img src="/includes/emoticons/'.$image.'">', $contents); }
                  

                  The whole point is to replace :someface: with the related someface.gif image in my little chat room. Now, it works for one instance and one only. If I do 😄 it works, but the next time I do 😄 or any other face, the previous link turns into a bad link and has the value "Array" in it instead of the image link. I know I'm missing something small and obvious here.

                    I actually gave you the answer 🙂

                    $smileys = array();
                    while (list ($key, $val) = each ($filelist)) {
                    $oldval = $val;
                    $val = basename($val, ".gif") ;
                    $val = basename($val, ".png") ;
                    $val = basename($val, ".jpg") ;
                    $smileys[":".$val.":"] = $oldval;
                    }
                    

                    On the programming practices side (as you say you're new to PHP), using foreach() instead of while() makes for slightly cleaner code - especially since you don't need $key.

                    Also, you don't need to sort $filelist (the same replacements will be made no matter what order they appear in the list in).

                    Instead of using basename() (since you don't have any path information to strip), just lop off everything after the last '.' inclusive $val = substr($val, 0, strrpos($val,'.'));. This assumes that you know everything in that directory is going to be the right sort of thing - but you're assuming that already.

                    Since you don't need to sort the file list, you can combine the two loops and discard the $filelist array completely. Get a file name from the directory handle, put an entry in the $smileys array, get another name, ....

                      Originally posted by Weedpacket
                      I actually gave you the answer 🙂

                      $smileys = array();
                      while (list ($key, $val) = each ($filelist)) {
                      $oldval = $val;
                      $val = basename($val, ".gif") ;
                      $val = basename($val, ".png") ;
                      $val = basename($val, ".jpg") ;
                      $smileys[":".$val.":"] = $oldval;
                      }
                      

                      On the programming practices side (as you say you're new to PHP), using foreach() instead of while() makes for slightly cleaner code - especially since you don't need $key.

                      Also, you don't need to sort $filelist (the same replacements will be made no matter what order they appear in the list in).

                      Instead of using basename() (since you don't have any path information to strip), just lop off everything after the last '.' inclusive $val = substr($val, 0, strrpos($val,'.'));. This assumes that you know everything in that directory is going to be the right sort of thing - but you're assuming that already.

                      Since you don't need to sort the file list, you can combine the two loops and discard the $filelist array completely. Get a file name from the directory handle, put an entry in the $smileys array, get another name, .... [/B]

                      Excellent information Weedpacket. I really do appreciate the help.

                      Once of the reasons I chose to use basename was in the off chance there might come a situation in the future where there is path information, and I would have it kind of covered already. Do you suppose that strrpos is worth implementing in place of basename overall? (Speed, etc).

                      Also, I chose to sort the list like that so I could generate a page of available icons for use with that code as well. You are correct though, it's definately serving no purpose in the actual display process. 🙂

                      Thanks again.

                      -Sys

                        Write a Reply...