基本上我想使用RegEx来抓取文档中各段之间的内容.我认为表达式是:
<p>.+?</p>
Run Code Online (Sandbox Code Playgroud)
假设它使用此RegEx抓取10个项目,然后我希望PHP随机选择其中一个,然后将其保存到变量.有任何想法吗?
// Test data
$str = '<p>a1</p><p>b2</p><p>c3</p><p>d4</p>';
// Pull out all the paragraph contents into $matches
preg_match_all('_<p>(.+?)</p>_is', $str, $matches);
// $matches[0] contains all the <p>....</p>
// $matches[1] contains the first group, i.e. our (.+?)
// Echo a random one
echo $matches[1][array_rand($matches[1])];
Run Code Online (Sandbox Code Playgroud)