0

I have html code in variable:

$html = '<html>
  <a href="http://example.com/extnu/111"></a>
  <a href="http://example.com/extnu/222"></a>
</html>';

How do I search for /extnu/ and find the first URL containing that term? So, the result should be http://example.com/extnu/111;

Henrik Petterson
  • 6,776
  • 19
  • 65
  • 144
  • 1
    what did you research/try before coming to SO? – treyBake Feb 03 '20 at 13:59
  • 1
    @treyBake I tried string search. I could match the exact term `/extnu/` but couldn't figure out how to _get_ the full URL. – Henrik Petterson Feb 03 '20 at 14:00
  • As always, [using Regex to parse HTML is a bad idea](https://stackoverflow.com/a/1732454/1481699). However since the circumstance seems trivial, you could try something like `href="([^"]+/extnu/[^"]+)"`. That should get you the URL in the first Regex group. – AlphaDelta Feb 03 '20 at 14:02
  • From the entry point of your application parse your url `$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)`; check your `$uri` variable. – udit rawat Feb 03 '20 at 14:37

0 Answers0