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

PHP用空格替换每个非字母数字字符,php,preg-replace,Php,Preg Replace,我找了又找。我找不到解决办法。我有一个类似这样的字符串:ABC_test 001-2.jpg 我还有一段代码: $makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", " ", $replaceUnder); 但是,这段代码不会替换下划线(389;)。事实上,这个变量的输出是:ABC 因此,一旦它碰到下划线,它就会停止。我需要替换所有可能的非字母数字字符,包括下划线、星号、问号等等。我错过了什么 谢谢你的帮助 编辑: 我无法重现您的问题,对

我找了又找。我找不到解决办法。我有一个类似这样的字符串:ABC_test 001-2.jpg

我还有一段代码:

$makeSpace = preg_replace("/[^a-zA-Z0-9\s]/", " ", $replaceUnder);
但是,这段代码不会替换下划线(389;)。事实上,这个变量的输出是:ABC

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

谢谢你的帮助

编辑:


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

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

我觉得这段代码不错。你确定
$replacedunder
包含你认为它所包含的内容(并且你真的在查看
$makeSpace
包含的内容)?废话,为什么没有一条规则强制OPs发布可复制的代码?为什么我要自己写呢?老兄。不要发布你的代码。你必须试试arnorhs的代码。告诉我们结果我试过了,但结果都一样。我的代码与我发布的原始代码有点不同,所以我想发布所有代码,以防万一……好吧,这就是我要做的。我正在成功地从一个目录中提取所有文件名,然后使用这些文件名应用类和rel。这是我的全部代码。这是我的代码
$directory='ui/images/customFabrication/';尝试{//create slideshow div由上述jquery函数echo“”;echo“”;echo“
    ”;//遍历目录,获取图像,设置路径并在img标记中回显它们。foreach(新的DirectoryIterator($directory)作为$item){
如果($item->isFile()){$path=$directory.““$item;$class=substr($item,0,-4);//从文件名中删除文件类型//$replaceUnder=str_replace(“,”,“-”,$class);$makeDash=str_replace(“,“,”,$replaceUnder)
$replaceUnder=preg\u replace(“/[^a-zA-Z0-9\s]/”,“,$class”);//$makeSpace=preg\u replace(“/[^a-zA-Z0-9\s]/”,“,$replaceUnder”);echo“
  • 哇。很抱歉这些疯狂的评论。我将编辑我的原始帖子。
    <?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';
    }
    ?>