-1

I'll get right into it....

I have a problem with my ajax livesearch, using xml for a kind of inventory image search function, the code sees the image filenames imported into an xml file for use in the livesearch, however, image filenames with some special characters like a "-" is causing the livesearch to not work. I have provided the code below...

What I would like to do is code in a preg_replace to clean the filenames of these types of special characters, ideally use only letters, numbers, periods and the dollar sign. I'm not sure if it is wise to clean the filenames before they get imported to the xml file or clean them afterwards? Any help is appreciated, thx.

<?php

$path_to_image_dir = 'images'; // relative path to your image directory


$xml_string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<images> 
</images>
XML;

$xml_generator = new SimpleXMLElement($xml_string);

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path_to_image_dir));
foreach($it as $path => $file) {
    if($file->isDir()) continue; // skip folders
    list( $width, $height ) = getimagesize($path);
    $image = $xml_generator->addChild('image');
    $image->addChild('path', $path);
    $image->addChild('thumbnail', "<img src='$path' />");
}

$file = fopen('data.xml','w');
fwrite($file, $xml_generator->asXML());
fclose($file);?>

<!-- php code for initiating instant search of xml data file -->

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("data.xml");

$x=$xmlDoc->getElementsByTagName('image'); //used to be link, now image, could be images

//get the q parameter from URL
$q=$_GET["q"];


//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
  $hint="";
  for($i=0; $i<($x->length); $i++) {

    $y=$x->item($i)->getElementsByTagName('path');
    $z=$x->item($i)->getElementsByTagName('thumbnail');
    $w=$x->item($i)->getElementsByTagName('image');
    if ($y->item(0)->nodeType==1) {
      //find a link matching the search text
      if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
        if ($hint=="") {
           $hint="<a style='font: bold 16px/18px century gothic, serif;' href=' 
           ".$y->item(0)->childNodes->item(0)->nodeValue."' target='_blank'>
           ".$y->item(0)->childNodes->item(0)->nodeValue."<br>
           ".$z->item(0)->childNodes->item(0)->nodeValue." <br><br>
        ";
        }
      else
        {
         $hint=$hint ."<a style='font: bold 16px/18px century gothic, serif;' href=' 
         ".$y->item(0)->childNodes->item(0)->nodeValue."' target='_blank'>
         ".$y->item(0)->childNodes->item(0)->nodeValue."<br>
         ".$z->item(0)->childNodes->item(0)->nodeValue." <br><br>
        ";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="") {
  $response="Search found ' 0 ' matches";
} else {

  $response=$hint;
}

//output the response
echo $response;
?>

I tried adding this to clean the filenames but it didn't work:

function clean($string) {
   $string = str_replace('-', 'test', $string); 

   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); 
}

// and then change the echo to this... (but didn't work as I planned,

echo clean ($response);
WebEducate
  • 79
  • 2
  • 17

1 Answers1

0

This can be accomplished with str_replace():

function clean($string) {
    $forbidden = array('-','#', ',');
    $clean = str_replace($forbidden, '_', $string);

   return $clean; 
}

To Implement:

   echo clean('test-img-101.png'); //outputs test_img_101.png
AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • Please forgive my coding skills are quite limited, could you show how to implement it? – WebEducate Nov 18 '14 at 18:17
  • 1
    Apologies, please try updated code exactly as it is above. In your edited post, you're using **both** `str_replace` and `preg_replace`- you don't need both. Replace the `preg_replace` line with `return $string;` – AnchovyLegend Nov 18 '14 at 18:24
  • Yes it worked, however the image is now broken because the filenames dont match (i assume). I thought that might happen. Is there a way to alter the code to clean the filenames before they get imported to the xml file? – WebEducate Nov 18 '14 at 18:27
  • The path for the file obviously needs to match the name of the file you're linking to. – AnchovyLegend Nov 18 '14 at 18:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65167/discussion-between-webeducate-and-anchovylegend). – WebEducate Nov 18 '14 at 18:29