PHP 用空格替换每个非字母数字字符
我已经找了又找了。我找不到解决方案。我有一个字符串,有点像这样: 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]/", " ", $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法重现您的问题,对我来说输出的字符串是:
。
调试你的代码
我浏览了你粘贴的代码,发现了一些错误,这些错误可能相关,也可能不相关:
我在这一行收到错误,因为replaceUnder未定义:
因为你注释掉了这一行:
我猜你的意思是也将其注释掉。根本不清楚你想要做什么以及为什么你有所有这些替换语句。如果您只是想用替换的所有符号来回显文件名,这就是我所做的,并且所有字母都被空格替换:
我认为您的整个问题源于变量的命名。考虑打开通知错误 - 如果您引用未定义的变量,它们会让您知道。
I can't reproduce your problem, for me the string that gets outputted is:
.
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:
since you commented this line out:
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:
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.