【发布时间】:2011-03-12 06:59:00
【问题描述】:
我已经搜索和搜索。我找不到解决方案。我有一个类似这样的字符串: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';
}
?>
【问题讨论】:
-
这段代码对我来说很好。您确定
$replaceUnder包含您认为的内容(并且您确实在查看$makeSpace包含的内容)吗? -
废话,为什么没有规则要求 OP 发布可重现的代码? 为什么我要自己写?
-
老兄。不要发布您的代码。 您必须改用 arnorhs 的代码。并告诉我们结果
-
我试过了,但结果是一样的。我的代码与我发布的原始代码有点不同,所以我想将其全部发布以防万一......
标签: php preg-replace