【问题标题】:preg_replace to clean my image filenamespreg_replace 清理我的图像文件名
【发布时间】:2014-11-18 18:13:02
【问题描述】:

我会直接进入它......

我的 ajax livesearch 有问题,使用 xml 作为一种库存图像搜索功能,代码看到导入到 xml 文件中的图像文件名以用于 livesearch,但是,图像文件名带有一些特殊字符,如"-" 导致实时搜索无法工作。我已经提供了下面的代码...

我想做的是在 preg_replace 中编写代码来清除这些特殊字符类型的文件名,最好只使用字母、数字、句点和美元符号。我不确定在文件名被导入到 xml 文件之前清理文件名或之后清理它们是否明智?感谢您提供任何帮助,谢谢。

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

我尝试添加它来清理文件名,但它不起作用:

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

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

//然后把echo改成这个……(但没有按我的计划工作,

echo clean ($response);

【问题讨论】:

    标签: php xml preg-replace filenames strip


    【解决方案1】:

    这可以通过str_replace()来完成:

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

    实施:

       echo clean('test-img-101.png'); //outputs test_img_101.png
    

    【讨论】:

    • 请原谅我的编码能力很有限,你能告诉我如何实现吗?
    • 抱歉,请按照上面的方式尝试更新代码。在您编辑的帖子中,您使用 both str_replacepreg_replace- 您不需要两者。将preg_replace 行替换为return $string;
    • 是的,它有效,但是图像现在已损坏,因为文件名不匹配(我假设)。我认为这可能会发生。有没有办法在将文件名导入 xml 文件之前更改代码以清理文件名?
    • 文件的路径显然需要与您链接的文件的名称相匹配。
    猜你喜欢
    • 2016-05-04
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多