PHP 用空格替换每个非字母数字字符

发布于 2024-10-21 06:44:19 字数 1907 浏览 1 评论 0原文

我已经找了又找了。我找不到解决方案。我有一个字符串,有点像这样: 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';
        }
        ?>

I've searched and searched. I can't find the solution. I have a string that goes a little something like this: ABC_test 001-2.jpg

I also have this bit of code:

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

However, this bit of code will not replace the underscore (_). In fact, the output of this variable is: ABC

So it stops once it hits the underscore. I need to replace EVERY possible non-alphanumeric character, including the underscore, asterisks, question marks, whatever. What am I missing?

Thanks for the help.

EDIT:

<?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]/", " ", $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';
        }
        ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七秒鱼° 2024-10-28 06:44:19

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

$replaceUnder = 'ABC_test 001-2.jpg';
$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", " ", $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';
}
?>

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

I can't reproduce your problem, for me the string that gets outputted is:

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

# output:
# ABC test 001 2 jpg

.

dubugging your code

I went through the code you pasted in and found a few errors, which are maybe related, maybe not:

I get an error on this line, because replaceUnder is not defined:

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

since you commented this line out:

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

I guess you meant to comment it out as well. It's not clear at all what you're trying to do and why you have all those replace statements. If you're just trying to echo out the file names with all the symbols replaced, this is how I did it and the letters all got replaced with spaces:

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

I think your whole problems stem from the naming of variables. Consider turning on notice errors - they will let you know if you're referencing variables that aren't defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文