[英]PHP Regular expression tag matching
一直在我的头上碰壁,试图使它正常工作-来自任何正则表达式专家的帮助将不胜感激!
必须匹配的文本
[template option="whatever"]
<p>any amount of html would go here</p>
[/template]
我需要在模板标签之间提取“ option”值(即“ whatever”)和html。
到目前为止,我有:
> /\[template\s*option=["\']([^"\']+)["\']\]((?!\[\/template\]))/
除了模板标签之间的html之外,这让我得到了所有东西。
有任何想法吗?
谢谢克里斯
尝试这个
/\[template\s*option=\"(.*)\"\](.*)\[\/template]/
基本上,而不是使用复杂的正则表达式来匹配每件事,只需使用(。*),这意味着全部,因为您想要介于两者之间的所有内容,而不是想要验证介于两者之间的数据
编辑:[\\ s \\ S]将匹配任何空格或非空格。
大字符串中有连续的块时,您可能会遇到问题。 在这种情况下,您将需要指定更具体的量词-非贪婪(+?)或指定范围{1,200}或使[\\ s \\ S]更具体
/\[template\s*option=["\']([^"\']+)["\']\]([\s\S]+)\[\/template\]/
断言?!
不需要方法。 只需与.*?
匹配.*?
得到最小的内脏。
/\[template\s*option=\pP([\h\w]+)\pP\] (.*?) [\/template\]/x
克里斯,
我知道您已经接受了答案。 大!
但是,我认为在这里使用正则表达式不是正确的解决方案。 我认为您可以通过使用字符串操作(子字符串等)来获得相同的效果
这是一些可以帮助您的代码。 如果不是现在,也许稍后再进行编码。
<?php
$string = '[template option="whatever"]<p>any amount of html would go here</p>[/template]';
$extractoptionline = strstr($string, 'option=');
$chopoff = substr($extractoptionline,8);
$option = substr($chopoff, 0, strpos($chopoff, '"]'));
echo "option: $option<br \>\n";
$extracthtmlpart = strstr($string, '"]');
$chopoffneedle = substr($extracthtmlpart,2);
$html = substr($chopoffneedle, 0, strpos($chopoffneedle, '[/'));
echo "html: $html<br \>\n";
?>
希望这可以帮助任何寻求不同口味类似答案的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.