繁体   English   中英

preg_match-结束标记和开始标记之间的文本

[英]preg_match - text between closing and opening tag

我的任务很奇怪。

我需要使用PHP中的preg_match()函数从html标记中获取文本。 问题是我需要的文本介于关闭和打开html标签之间,或带有标签的文本之间。

以下是我的html字符串:

<h2>Title of post</h2> 1 category <strong>task 1</strong> 1 category <strong>task 2</strong> 1 category <strong>task 3</strong>&nbsp; 

更具体地说:我需要在</h2><strong>标记之间使用字符串“ 1 category”。

当我尝试在打开标签和关闭标签之间抓取文本时,它工作正常,并且正在使用以下功能:

preg_match_all('#<strong>(.*?)</strong>#',$string,$matches);

我尝试了多种组合以在关闭标签和打开标签之间获取文本。 他们都没有解决。 我已经结束使用这样的功能了:

preg_match_all('#<\/strong>(.*?)<strong>#',$content,$matches_all);

没有结果。

奇怪的是,在在线正则表达式测试器上,具有上述功能和上述功能的功能有时会起作用。

我的图案不好吗? 我是否缺少一些标志? 您知道以这种方式获取文字的最佳方法是什么吗? 不幸的是,我与Regex方法有关,在我的情况下不允许使用XMLDomParser之类的解决方案。

非常感谢您的帮助。

试试这个。

preg_match_all('/<([^>]+)>(?:([^<]+))*(?=[^>]*\<)/',$string,$matches);

现场演示

看起来您的php安装/配置有问题。

您的代码原样。

$content = '<h2>Title of post</h2> 1 category <strong>task 1</strong> 1 category <strong>task 2</strong> 1 category <strong>task 3</strong>&nbsp;'; 
preg_match_all('#<\/h2>(.*?)<strong>#',$content,$matches);
print_r($matches);

输出:

Array
(
    [0] => Array
        (
            [0] => </h2> 1 category <strong>
        )

    [1] => Array
        (
            [0] =>  1 category 
        )

)

现场演示

注意 :由于您的模式只有一个匹配项(在</h2> <strong> ),因此您可以像$maches[1][0]或使用preg_match

如果要在结束标记和开始标记之间插入所有文本,则可以使用此代码。 请注意,我更改了文本,以使每组关闭/打开标签之间的文本都不同,从而更明显地是匹配项正在查找每个值。

$str = '<h2>Title of post</h2> 1 category <strong>task 1</strong> 2 category <strong>task 2</strong> 3 category <strong>task 3</strong> ';
preg_match_all('#(?:</[^>]+>)(.*?)<#', $str, $matches);
print_r($matches[1]);

输出:

Array
(
    [0] =>  1 category 
    [1] =>  2 category 
    [2] =>  3 category 
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM