와 unlink를 사용을 시도 할 수 있습니다 전에 몇 가지 실수와 확인 및/또는이있는 경우 입력에 대한 어느 정도의 통제가 있다면, 당신은 완전성에서 약간의 타협을하고 Regex를 사용할 수 있습니다. 당신의 업데이트는 이러한 경우라고 때문에, 여기 당신을 위해 작동해야 정규식입니다 :
/<a\s(?:.(?!=href))*?href="([^"]*)"[^>]*?>(.*?)</a>/gi
- $ (1) : href를
- $ 2 : 태그 내부의 모든.
이 지난 3 개 라인을 제외하고 아래의 모든 테스트 케이스를 처리합니다 :
Hello this is some text <a href="/test">This is a link</a> and this is some more text.
<a href="/test">Just a link on this line.</a>
There are <a href="/test">two links </a> on <a href="http://www.google.com">this line</a>!
Now we need to test some <a href="http://www.google.com" class="test">other attributes.</a>. They can be <a class="test" href="http://www.google.com">before</a> or after.
Or they can be <a rel="nofollow" href="http://www.google.com" class="myclass">both</a>
Also we need to deal with <a href="/test" class="myclass" style=""><span class="something">Nested tags and empty attributes</span></a>.
Make sure that we don't do anything with <a name="marker">anchors with no href</a>
Make sure we skip other <address href="/test">tags that start with a even if they are closed with an a</a>
Lastly try some other <a href="#">types</a> of <a href="">href</a> attributes.
Also we need to skip <a malformed tags. </a>. But <a href="#">this</a> is where regex fails us.
We will also fail if the user has used <a href='javascript:alert("the reason"))'>single quotes for some reason</a>
Other invalid HTML such as <a href="/link1" href="/link2">links with two hrefs</a> will have problems for obvious reasons.
당신이 정규식을 사용하지 않는, HTML을 조작해야합니다. –
인수로 들어가는 것은 그냥 위생 처리 이거나 다른 비트가있을 것이라고 확신 할 수 있습니까? – Layke
너무 모호합니다. 어떤 언어, 환경? –