콘텐츠의 URL을 쉽게 찾을 수있는 정규식이 있습니다. 목표는 "/ folder/id/123"과 같은 링크에서 폴더를 제거하고 "id/123"으로 바꾸는 것입니다. 따라서이 폴더는 동일한 폴더에서 상대적으로 짧습니다. Regex : negative assertion
는 사실은 내가$pattern = "/\/?(\w+)\/id\/(\d)/i"
$replacement = "id/$2";
return preg_replace($pattern, $replacement, $text);
를하고 그것을 잘 작동하는 것 같다.
그러나 마지막 테스트는 동일한 패턴/folder/id/123을 사용하는 외부 사이트 인 경우 일치하는 각 URL이 http : //를 포함하지 않는지 테스트하는 것입니다.
나는/[^ http : //] 또는 (? <! html) ...과 다른 것들을 성공하지 못했습니다. 어떤 도움이라도 좋을 것입니다 :-)
$pattern = "/(?<!http)\b\/?(\w+)\/id\/(\d)/i"; ???????
고마워요! 여기
은 몇 가지 예입니다 :(these should be replaced, "same folder" => short relative path only)
<a href="/mysite_admin/id/414">label</a> ==> <a href="id/414">label</a>
<a href="/mYsITe_ADMIN/iD/29">label with UPPERCASE</a> ==> <a href="id/414">label with UPPERCASE</a>
(these should not be replaced, when there is http:// => external site, nothing to to)
<a href="http://mysite_admin/id/414">label</a> ==> <a href="http://mysite_admin/id/414">label</a>
<a href="http://www.google_admin.com">label</a> ==> <a href="http://www.google_admin.com">label</a>
<a href="http://anotherwebsite.com/id/32131">label</a> ==> <a href="http://anotherwebsite.com/id/32131">labelid/32131</a>
<a href="http://anotherwebsite_admin.com/id/32131">label</a> ==> <a href="http://anotherwebsite_admin.com/id/32131">label</a>
, 예를 제공하십시오. 당신의 작업은 간단하게 들리지만 검사 할 문자열을 볼 수 없다. – gaussblurinc
하나의 정규식을 사용하여 url이 http와 다른 정규 표현식으로 시작하는지, PHP if 문 안에 있는지를 확인하기 위해 똑같은 일을 할 수있다. 바꿔 놓음. 글을 쓰고 이해하는 것이 더 쉽고, 복잡한 pcre는 아마도 더 효율적이지 않습니다. – Vortexfive