PDA

View Full Version : PHP: get URL and link name with regular expressions?


Paperbag
27th December 07, 08:46 AM
I have the following string:

<li><a href="index.php?site=home">Front page</a></li>

I need to get the two things marked in blue. Either as two separate variables or as an array ([0] and [1]).

Can anybody tell me the exact regular expression to use with preg_match to do this?

I'm clueless when it comes to regular expressions and can't find any simple documentation for this.

cchana
27th December 07, 09:19 AM
do want these after they've been written to screen or when you click on the link?

you could assign them both to an array as you go through the loop that writes them out?

Paperbag
27th December 07, 11:49 AM
do want these after they've been written to screen or when you click on the link?
Neither. This line is one of many in a .html file on the same server. I open it with fopen and read each line with fgets. From there on I need to split up these lines as I explained above.

chrishirst
30th December 07, 04:04 PM
to match the anchor text of the link the pattern will be

(">).*(</a)

you will need to clean up the result with a replace because it will include the "> and </a

to match the querystring portion

(\bsite=\b).*(")

would work but again a bit of cleaning up will be needed.

The clean up is due to regular expresions being "greedy" and returning the longest string it can.

Paperbag
3rd January 08, 08:09 PM
Thanks.