我正在尝试使用正则表达式替换书籍数组中随机拾取的元素的所有字母字符和“ - ”字符。我不确定我做错了什么
<?php
$books = array("Dune <br/> ~Frank Herbert",
"Romeo & Juliet <br/> ~William Shakespeare",
"Sherlock Holmes <br /> ~Sir Arthur Conan Doyle",
"The Iliad <br/> ~Homer");
$randNum = array_rand($books, 1);
$pattern = '[A-Za-z]';
echo preg_replace($pattern, '-', $books[$randNum]);
?>
答案 0 :(得分:0)
这是你的代码尝试这将起作用 -
<?php
$books = array("Dune <br/> ~Frank Herbert",
"Romeo & Juliet <br/> ~William Shakespeare",
"Sherlock Holmes <br /> ~Sir Arthur Conan Doyle",
"The Iliad <br/> ~Homer");
$randNum = array_rand($books, 1);
$pattern = '/[A-Za-z]/';
echo preg_replace($pattern, '-', $books[$randNum]);
?>