Wir*_*hod 10 php preg-match-all preg-match
我想在<span> </span>HTML中的所有标记之间获取数组中的文本,我已尝试使用此代码,但它只返回一次:
preg_match('/<span>(.+?)<\/span>/is', $row['tbl_highlighted_icon_content'], $matches);
echo $matches[1];
Run Code Online (Sandbox Code Playgroud)
我的HTML:
<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way,
Run Code Online (Sandbox Code Playgroud)
我的代码只返回一次span标记,但我希望以php数组的形式从HTML中的每个span标记中获取所有文本.
您需要切换到preg_match_all函数
代码
$row['tbl_highlighted_icon_content'] = '<span>The wish to</span> be unfairly treated is a compromise attempt that would COMBINE attack <span>and innocen</span>ce. Who can combine the wholly incompatible, and make a unity of what can NEVER j<span>oin? Walk </span>you the gentle way,';
preg_match_all('/<span>.*?<\/span>/is', $row['tbl_highlighted_icon_content'], $matches);
var_dump($matches);
Run Code Online (Sandbox Code Playgroud)
正如您现在所看到的,array已正确填充,因此您可以进行echo所有匹配