1

I need a regex that will give me the string inside an href tag

For example i need to extract NAME in the following:

<a href="Somedomain1.com">NAME</a>

And the link inside href is not constant

My regex

'/<a href="(.+)">/'

it give me the link inside the href only. and I don't know how to get the name of a url

Thank you in advance

2 Answers 2

1
'<a\shref="(.*)">(.*)<\/a>'

echo matches[1]; // url
echo matches[2]; // NAME
Sign up to request clarification or add additional context in comments.

Comments

1

Using RegEx is strongly not recommended for parsing HTML. It would be much better if you do that by a HTML parser. This is my favorite. Anyway, if you insist on to do this by RegEx, try this pattern:

/<a href=[^>]+>(.*)<\/a>/

Now, $1 contains expected result.

Online Demo

1 Comment

Thank you, for your answer that regex did work so thanks for that. and thank you for suggesting another method as I am new to programming I didin't know other methods exists so I will definitely check the HTML Parser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.