我在WP中有字幕,我需要抓取文本。
最初,它们看起来像这样:
[caption id="attachment_16689" align="aligncenter" width="754"]<a href="http://www.site.com/" target="_blank"><img class=" wp-image-16689 " title="Title" src="http://site.com/wp-content/uploads/2012/11/image.jpg" alt="" width="754" height="960" /></a> I want to get this text out of here.[/caption]
我能够删除图片和标签:
$c = preg_replace(array("/<img[^>]+\>/i", "/<a[^>]*>(.*)<\/a>/iU"), "", $caption);
离开:
[caption id="attachment_16689" align="aligncenter" width="754"] I want to get this text out of here.[/caption]
我希望能够从标题中删除“我希望将此文本从此处删除”。我尝试了许多不同的表达方式,但我都无法解决这些问题。在上面的代码之后我有:
preg_replace('/(\[caption.*])(.*)(\[/caption\])/', '$1$3', $c);
我做错了什么?
答案 0 :(得分:2)
见下文,您实际上想要替换第二个主题。此外,你有一些误逃的反斜杠。
$caption = '[caption id="attachment_16689" align="aligncenter" width="754"]<a href="http://www.site.com/" target="_blank"><img class=" wp-image-16689 " title="Title" src="http://site.com/wp-content/uploads/2012/11/image.jpg" alt="" width="754" height="960" /></a> I want to get this text out of here.[/caption]';
$c = preg_replace(array("/<img[^>]+\>/i", "/<a[^>]*>(.*)<\/a>/iU"), "", $caption);
$c = preg_replace('%(\\[caption.*])(.*)(\\[/caption\\])%', '$2', $c);
您还可以通过将标题替换添加到数组中来稍微减少它。只要确保第二个参数也是一个数组array('','','$2')
$caption = '[caption id="attachment_16689" align="aligncenter" width="754"]<a href="http://www.site.com/" target="_blank"><img class=" wp-image-16689 " title="Title" src="http://site.com/wp-content/uploads/2012/11/image.jpg" alt="" width="754" height="960" /></a> I want to get this text out of here.[/caption]';
$c = preg_replace(array("/<img[^>]+\>/i", "/<a[^>]*>(.*)<\/a>/iU",'%(\\[caption.*])(.*)(\\[/caption\\])%'), array('','','$2'), $caption);
答案 1 :(得分:1)
$string = '[caption id="attachment_16689" align="aligncenter" width="754"] I want to get this text out of here.[/caption]';
preg_match("/\[caption.*\](.*?)\[\/caption\]/",$string,$matches);
$caption_text = $matches[1];
echo $caption_text;