有人可以提出一种简洁/优雅的方法来从Java字符串中修剪前导和尾随标点字符(Java regex <?php
$files = glob("images/*.*");
for ($i=0; $i<count($files); $i++)
{
$image = $files[$i];
$supported_file = array(
'gif',
'jpg',
'jpeg',
'png'
);
$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
echo basename($image)."<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
} else {
continue;
}
}
?>
或POSIX regex \p{Punct}
)吗?保留在字符串“ body”内的标点字符(即,字符串中的第一个和最后一个非标点字符之间)。
通过将String转换为字符数组并在前后方向上循环并跳过标点符号,直到遇到第一个非标点字符,我确实可以通过这种方式工作。但是,我认为必须有一种更好的方法。