How to extract the text between anchor tag in PHP?

不羁的心 提交于 2019-12-02 09:39:54

Use preg_match_all function inorder to do a global match.

preg_match_all('~>\K[^<>]*(?=<)~', $str, $match);

Here preg_match would be enough. \K discards the previously matched characters from printing at the final, so it won't consider the previouslly matched > character. You could use a positive lookbehind instead of \K also , like (?<=>). [^<>]* Negated character class, which matches any character but not of < or >, zero or more times. (?=<), Positive lookahead assertion which asserts that the match must be followed by < character.

DEMO

$str = 'posted an event in <a href="http://52.1.47.143/group/186/">TEST PRA</a>';
preg_match('~>\K[^<>]*(?=<)~', $str, $match);
print_r($match[0]);

Output:

TEST PRA
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!