Home >Backend Development >PHP Tutorial >Regular Expression - How to use regular expression to get the content of a tag in php?
<code><!-- 1 --> <a class="myclass" target="_blank" href="http://www.taobao.com/">我是内容1</a> <!-- 2 --> <a target="_blank" class="myclass" href="http://www.baidu.com/">我是内容2</a> <!-- 3 --> <a class="noclass" target="_blank" href="http://www.foobar.com/">不包含我</a> </code>
Like the tag above, I want to get the content of 1 and 2 of the a tag (I am content 1, I am content 2), but I don’t want 3 (not including me). The only difference between them is that the class
tags are different class
location may also be different! ! ! **
How to use regular expression to obtain it?
<code><!-- 1 --> <a class="myclass" target="_blank" href="http://www.taobao.com/">我是内容1</a> <!-- 2 --> <a target="_blank" class="myclass" href="http://www.baidu.com/">我是内容2</a> <!-- 3 --> <a class="noclass" target="_blank" href="http://www.foobar.com/">不包含我</a> </code>
Like the tag above, I want to get the content of 1 and 2 of the a tag (I am content 1, I am content 2), but I don’t want 3 (not including me). The only difference between them is that the class
tags are different class
location may also be different! ! ! **
How to use regular expression to obtain it?
Thanks for the invitation.
<code class="php">$str = '<a class="myclass" target="_blank" href="http://www.taobao.com/">我是内容1</a> <a target="_blank" class="myclass" href="http://www.baidu.com/">我是内容2</a> <a class="noclass" target="_blank" href="http://www.miyahuo.com/">不包含我</a>'; preg_match_all('#<a .*class="myclass".*>(.*)</a>#', $str,$m); print_r($m); /* Array ( [0] => Array ( [0] => <a class="myclass" target="_blank" href="http://www.taobao.com/">我是内容1 <a target="_blank" class="myclass" href="http://www.baidu.com/">我是内容2 Array ( [0] => 我是内容1 [1] => 我是内容2 ) ) */</code>
Thanks for the invitation, correct answer upstairs