PHP中的随机正则表达式
基本上,我想使用正则表达式来获取文档中段落之间的内容。我想表达方式应该是:PHP中的随机正则表达式,php,regex,Php,Regex,基本上,我想使用正则表达式来获取文档中段落之间的内容。我想表达方式应该是: <p>.+?</p> + 假设它使用这个正则表达式抓取10个项目,我希望PHP随机选择其中一个,然后将其保存到一个变量中。有什么想法吗?//测试数据 // Test data $str = '<p>a1</p><p>b2</p><p>c3</p><p>d4</p>'; // Pull out al
<p>.+?</p>
+
假设它使用这个正则表达式抓取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])];
$str='a1b2c3d4';
//将所有段落内容提取到$matches中
preg_match_all(“(.+?)\u is',$str,$matches);
//$matches[0]包含所有
//$matches[1]包含第一个组,即我们的(+?)
//随声附和
echo$matches[1][array_rand($matches[1]);
//测试数据
$str='a1b2c3d4';
//将所有段落内容提取到$matches中
preg_match_all(“(.+?)\u is',$str,$matches);
//$matches[0]包含所有
//$matches[1]包含第一个组,即我们的(+?)
//随声附和
echo$matches[1][array_rand($matches[1]);