【问题标题】:PHP Replace every non-alphanumeric character with a spacePHP用空格替换每个非字母数字字符
【发布时间】:2011-03-12 06:59:00
【问题描述】:

我已经搜索和搜索。我找不到解决方案。我有一个类似这样的字符串:ABC_test 001-2.jpg

我也有这段代码:

$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", " ", $replaceUnder);

但是,这段代码不会替换下划线 (_)。其实这个变量的输出是:ABC

所以它一旦碰到下划线就会停止。我需要替换所有可能的非字母数字字符,包括下划线、星号、问号等等。我错过了什么?

感谢您的帮助。

编辑:

<?php
       //set images directory
        $directory = 'ui/images/customFabrication/';
        try {
            // create slideshow div to be manipulated by the above jquery function
            echo "<div class=\"slideLeft\"></div>";
                echo "<div class=\"sliderWindow\">";
                    echo "<ul id=\"slider\">";
            //iterate through the directory, get images, set the path and echo them in img tags.
            foreach ( new DirectoryIterator($directory) as $item ) {
                if ($item->isFile()) {
                    $path = $directory . "" . $item;
                    $class = substr($item, 0,-4); //removes file type from file name
                    //$replaceUnder = str_replace("_", "-", $class);
                    $makeDash = str_replace(" ", "-", $replaceUnder);

                    $replaceUnder = preg_replace("/[^a-zA-Z0-9\s]/", " ", $class);

                    //$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", "&nbsp;", $replaceUnder);
                    echo "<li><img rel=" . $replaceUnder . " class=" . $class . " src=\"/ui/js/timthumb.php?src=/" . $path . "&h=180&w=230&zc=1\" /></li>";
                }
            }
                    echo "</ul>";
                echo "</div>";
            echo "<div class=\"slideRight\"></div>";
        }
       //if directory is empty throw an exception.
        catch(Exception $exc) {
            echo 'the directory you chose seems to be empty';
        }
        ?>

【问题讨论】:

  • 这段代码对我来说很好。您确定$replaceUnder 包含您认为的内容(并且您确实在查看$makeSpace 包含的内容)吗?
  • 废话,为什么没有规则要求 OP 发布可重现的代码? 为什么我要自己写?
  • 老兄。不要发布您的代码。 您必须改用 arnorhs 的代码。并告诉我们结果
  • 我试过了,但结果是一样的。我的代码与我发布的原始代码有点不同,所以我想将其全部发布以防万一......

标签: php preg-replace


【解决方案1】:

我无法重现您的问题,对我来说输出的字符串是:

$replaceUnder = 'ABC_test 001-2.jpg';
$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", "&nbsp;", $replaceUnder);
print_r($makeSpace);

# output:
# ABC test 001 2 jpg

.

调试代码

我浏览了您粘贴的代码,发现了一些错误,可能相关,也可能无关:

我在这一行得到一个错误,因为 replaceUnder 没有定义:

$makeDash = str_replace(" ", "-", $replaceUnder);

既然你评论了这一行:

//$replaceUnder = str_replace("_", "-", $class);

我猜你也想把它注释掉。根本不清楚您要做什么以及为什么要使用所有这些替换语句。如果您只是想用替换所有符号来回显文件名,我就是这样做的,所有字母都被替换为空格:

<?php
//set images directory
$directory = './';
try {
    foreach ( new DirectoryIterator($directory) as $item ) {
        if ($item->isFile()) {
            $path = $directory . "" . $item;
            // remove ending/filetype - the other method doesn't support 4 letter file endings
            $name = basename($item);
            $fixedName = preg_replace("/[^a-zA-Z0-9\s]/", " ", $name);
            echo "Name: $fixedName\n";
        }
    }
            
}
//if directory is empty throw an exception.
catch(Exception $exc) {
    echo 'the directory you chose seems to be empty';
}
?>

我认为您的整个问题都源于变量的命名。考虑打开通知错误 - 如果您引用了未定义的变量,它们会通知您。

【讨论】:

  • 好的,这就是我想要做的。我成功地从目录中提取所有文件名,然后使用这些文件名应用类和 rels。这是我的完整代码。这是我的代码
  • $directory = 'ui/images/customFabrication/'; try { // create slideshow div to be manipulated by the above jquery function echo "&lt;div class=\"slideLeft\"&gt;&lt;/div&gt;"; echo "&lt;div class=\"sliderWindow\"&gt;"; echo "&lt;ul id=\"slider\"&gt;"; //iterate through the directory, get images, set the path and echo them in img tags. foreach ( new DirectoryIterator($directory) as $item ) {
  • if ($item-&gt;isFile()) { $path = $directory . "" . $item; $class = substr($item, 0,-4); //removes file type from file name //$replaceUnder = str_replace("_", "-", $class); $makeDash = str_replace(" ", "-", $replaceUnder);
  • $replaceUnder = preg_replace("/[^a-zA-Z0-9\s]/", " ", $class); //$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", "&amp;nbsp;", $replaceUnder); echo "&lt;li&gt;&lt;img rel=" . $replaceUnder . " class=" . $class . " src=\"/ui/js/timthumb.php?src=/" . $path . "&amp;h=180&amp;w=230&amp;zc=1\" /&gt;&lt;/li&gt;"; }
  • 哇。对疯狂的 cmets 感到抱歉。我将编辑我的原始帖子。
猜你喜欢
  • 1970-01-01
  • 2014-01-18
  • 2019-03-31
  • 2012-09-04
  • 2010-12-20
  • 2014-11-09
  • 2022-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多