Jump to content

Problems with links


ryanh_106

Recommended Posts

Hi,

 

I am using preg_match_all() I am trying to split the source for a hyperlink into the web address and the text.

e.g.

<a href="test.php">Test Page</a>

link:  test.php

text: Test Page

 

Can anyone help? Links may contain title="" attributes so I need to account for those, I originally had something like this:

preg_match_all('/<a href="(.*)"( title=(.*))?>(.*)<\/a>/', $newhtml, $matches); // Find all links

but this caused problems because if there was a title, the index of the link text in $matches moved up one so I couldnt extract it,

so i just did this:

preg_match_all('/<a (.*)>(.*)<\/a>/', $newhtml, $matches); // Find all links

which was fine as I only need to test the link address, I dont need to use it so if it had title="" at the end it didnt matter, but this doesnt work if there is HTML in the link text (such as <strong>)

How can I do this effectively?

Hope I explained that well enough

Please help, im getting really fed up of this!

 

Cheers

Link to comment
Share on other sites

Thanks for the assistance, unfortunately this would take out the <a> tags that I need also. I ended up solving this issue by ditching regex altogether. As follows:

- Split html by "<a"

- Split each result by "</a>"

- Finally split by ">"

This left me roughly with the link to be tested and the text to be dumped to screen.

 

Pretty messy but it works and this is urgent! thanks for the help anyway!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.