PHP Replace each non-alphanumeric character with a space

I searched and searched. I can not find a solution. I have a line that looks 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 does not replace underscore (_). In fact, the output of this variable is: ABC

Thus, he stops when he falls under the underscore. I need to replace EVERY possible non-alphanumeric character, including underscores, 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 directory you chose seems to be empty'; } ?> 
+4
source share
1 answer

I can not reproduce your problem, for me the line that is output 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 looked at the code you pasted and found some errors that might be related, maybe not:

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

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

since you commented on this line:

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

I think you also wanted to comment on this. It is not clear what you are trying to do and why you have all of these replacements. If you are just trying to revoke file names with all characters replaced, here is how I did it and all letters were 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 that all your problems stem from the name of the variables. Consider including error warnings - they will let you know if you are referring to variables that are not defined.

+4
source

Source: https://habr.com/ru/post/1343415/


All Articles