简体   繁体   English

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

[英]preg_match - text between closing and opening tag

I've very odd task to do. 我的任务很奇怪。

I need to grab text from html tags using preg_match() function in PHP. 我需要使用PHP中的preg_match()函数从html标记中获取文本。 Problem is that text I need is between closing and opening html tags or this text with tags. 问题是我需要的文本介于关闭和打开html标签之间,或带有标签的文本之间。

Below is my html string: 以下是我的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; 

To be more specific: I need string " 1 category " between </h2> and <strong> tag. 更具体地说:我需要在</h2><strong>标记之间使用字符串“ 1 category”。

When i try to grab text between opening and closing tags - It's working fine and I'm using this function: 当我尝试在打开标签和关闭标签之间抓取文本时,它工作正常,并且正在使用以下功能:

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

I've tried many combinations to get text between closing and opening tags. 我尝试了多种组合以在关闭标签和打开标签之间获取文本。 None of them worked out. 他们都没有解决。 I've ended using function like this: 我已经结束使用这样的功能了:

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

With no results. 没有结果。

The strange thins is that on online regex testers this function with above pattern with above function works sometimes. 奇怪的是,在在线正则表达式测试器上,具有上述功能和上述功能的功能有时会起作用。

Do I have bad pattern? 我的图案不好吗? Am I missing some flags? 我是否缺少一些标志? Do you know what can be best way to get text in this way? 您知道以这种方式获取文字的最佳方法是什么吗? Unfortunately I have to do with Regex approach, the solutions like XMLDomParser is not allowed in my case. 不幸的是,我与Regex方法有关,在我的情况下不允许使用XMLDomParser之类的解决方案。

Thanks a lot for help. 非常感谢您的帮助。

Try this one. 试试这个。

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

Live Demo 现场演示

Looks like something wrong with your php installation/configuration. 看起来您的php安装/配置有问题。

Your code as it's. 您的代码原样。

$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);

Output: 输出:

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

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

)

Live demo 现场演示

Note : Since there is only one match of your pattern ( between </h2> <strong> ) you can access like $maches[1][0] or use preg_match . 注意 :由于您的模式只有一个匹配项(在</h2> <strong> ),因此您可以像$maches[1][0]或使用preg_match

If you want all pieces of text between a closing and opening tag, you could use this code. 如果要在结束标记和开始标记之间插入所有文本,则可以使用此代码。 Note that I changed your text so that the text between each set of closing/opening tags was different so that it was more obvious that the match was finding each value. 请注意,我更改了文本,以使每组关闭/打开标签之间的文本都不同,从而更明显地是匹配项正在查找每个值。

$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]);

Output: 输出:

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

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

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