开发者

PHP Replace every non-alphanumeric character with a space

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]/", "&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 direct开发者_运维百科ory 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]/", "&nbsp;", $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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜