PHP正则表达式获取WP标题中的文本
我有在可湿性粉剂的标题,我需要抓取文本了 最初,它们看起来是这样的:PHP正则表达式获取WP标题中的文本,php,regex,wordpress,Php,Regex,Wordpress,我有在可湿性粉剂的标题,我需要抓取文本了 最初,它们看起来是这样的: [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/imag
[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]
我希望能够从标题中提取文本,并留下“我想从这里提取文本”。我试过许多不同的表达方式,但没有一种我能开始工作。在上述代码之后,我有:
preg_replace('/(\[caption.*])(.*)(\[/caption\])/', '$1$3', $c);
我做错了什么?见下文,您实际上想用第二个主题替换。还有,你的反斜杠漏掉了
$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;
$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);
您要提取的文本是否总是嵌入在相同的HTML标记中,而这将完成以下工作:“(.+)[/caption]”我想是的,但是这个客户端有1k+个帖子,所以我不能确定!啊,就是这样。我对preg_replace做了什么错误?据我所知,您没有逃逸第一个“]”。preg_replace('/[caption.*](.*)[\/caption]/','$1',$c)也可以使用。您仍然需要逃逸括号,但确实可以
$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;
$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);
$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);