-3

I need get the text from text in the html code using regex in php code. But maybe it's seem I wrong somewhere in my code. Please can you help me fix my code. Thank you very much! This is my regex pattern:

/<a\shref="\/vn\/tags\/.*">(?P<tags>.*)<\/a>/

And this is example subject:

<ul class="clearfix"><li><span class="tagBoxTitle">Từ khóa: </span></li><li><a href="/vn/tags/Th%E1%BB%B1c%20ph%E1%BA%A9m/index.html">Thực phẩm</a>,&nbsp;</li><li><a href="/vn/tags/gi%C3%A1-c%E1%BA%A3/index.html">giá-cả</a>,&nbsp;</li><li><a href="/vn/tags/h%C3%A0ng-ti%C3%AAu-d%C3%B9ng/index.html">hàng-tiêu-dùng</a>,&nbsp;</li><li><a href="/vn/tags/gi%E1%BA%A3m-gi%C3%A1/index.html">giảm-giá</a>,&nbsp;</li><li><a href="/vn/tags/c%C6%B0%E1%BB%9Bc-v%E1%BA%ADn-t%E1%BA%A3i/index.html">cước-vận-tải</a>,&nbsp;</li><li><a href="/vn/tags/gi%C3%A1-x%C4%83ng/index.html">giá-xăng</a>,&nbsp;</li><li><a href="/vn/tags/x%C4%83ng-d%E1%BA%A7u/index.html">xăng-dầu</a>,&nbsp;</li><li><a href="/vn/tags/h%C3%A0ng-h%C3%B3a/index.html">hàng-hóa</a>,&nbsp;</li><li><a href="/vn/tags/CPI/index.html">CPI</a>,&nbsp;</li><li><a href="/vn/tags/T%E1%BA%BFt-nguy%C3%AAn-%C4%91%C3%A1n/index.html">Tết-nguyên-đán</a></li></ul>

Hope I can get an answer as soon as possible, thanks again!

Duy Nguyen
  • 334
  • 4
  • 10

1 Answers1

0

Don't parse html with regex.

If you wanna regex solution then turn all the .* to .*? in-order to do a non-greedy match.

<a\shref="\/vn\/tags\/.*?">(?P<tags>.*?)<\/a>

DEMO

Community
  • 1
  • 1
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274