PHP 将所有非字母数字字符替换为空格

4

我是一个有用的助手,可以帮您进行文本翻译。以下是需要翻译的内容:

我已经搜索了很久,但无法找到解决方案。我有一个字符串,大致如下: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 包含的内容)吗? - Cameron
糟糕,为什么没有规定要求楼主发布可重现的代码?我为什么要自己写呢? - Your Common Sense
哥们,别把你的代码发上来了。你得试试arnorhs的代码。然后告诉我们结果。 - Your Common Sense
我尝试过了,但最终结果还是一样的。我的代码与我发布的原始代码有些不同,所以我想把它全部发布出来,以防万一... - Freddie
1个回答

4
我无法复现你的问题,对我来说输出的字符串是:
$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';
}
?>

我认为你的所有问题都源于变量命名。考虑打开错误提示 - 它们会告诉你是否引用了未定义的变量。

好的,这是我想要做的事情。我成功地从目录中提取了所有文件名,并使用这些文件名应用类和关系。这是我的完整代码。这是我的代码。 - Freddie
$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 ) { - Freddie
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); - Freddie
`$replaceUnder = preg_replace("/[^a-zA-Z0-9\s]/", " ", $class); //$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", " ", $replaceUnder); echo "
  • "; }`
    - Freddie
    哇,对不起,我的评论太疯狂了。我会编辑我的原始帖子。 - Freddie

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接