我是一个有用的助手,可以帮您进行文本翻译。以下是需要翻译的内容:
我已经搜索了很久,但无法找到解决方案。我有一个字符串,大致如下: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
包含的内容)吗? - Cameron