Php 如何使用正则表达式将标记html保存到变量
我有一个函数Php 如何使用正则表达式将标记html保存到变量,php,regex,Php,Regex,我有一个函数用于生成html标记: <p> <a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg"> <img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wor
用于生成html标记:
<p>
<a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg">
<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
</a>
</p>
我正在尝试提取
如何将完整的img标签设置为变量$StrImg
非常感谢。因此,您希望获得帖子中所有图像的src属性。有更简单的方法:
//$String =
<p>
<a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg">
<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
</a>
</p>
$String = "the_content()";
$pattern = '/^<img/.*/$/>/';//Take string wich start with "<img" and all between and End in "/>"
preg_match($pattern, substr($subject,3), $StrImg, PREG_OFFSET_CAPTURE);
echo $StrImg[0];
output
//<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
因此,您希望获得帖子中所有图像的src属性。有更简单的方法:
//$String =
<p>
<a href="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg">
<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
</a>
</p>
$String = "the_content()";
$pattern = '/^<img/.*/$/>/';//Take string wich start with "<img" and all between and End in "/>"
preg_match($pattern, substr($subject,3), $StrImg, PREG_OFFSET_CAPTURE);
echo $StrImg[0];
output
//<img class="alignnone size-full wp-image-82" title="page1-img2" src="http://127.0.0.1/www/www1/wordpress/wordpressEng/wp-content/uploads/2012/09/page1-img2.jpg" alt="" width="219" height="124" />
启用错误报告。然后删除多余的正斜杠,这是您的分隔符。还有更简单的方法从HTML中提取代码片段;DOM前端的效率较低,但如果您不使用正则表达式,则通常更可取。启用错误报告。然后删除多余的正斜杠,这是您的分隔符。还有更简单的方法从HTML中提取代码片段;DOM前端的效率较低,但如果您不使用正则表达式,则通常更可取。