개발! 딱 깔끔하고 센스있게!

php 로 문자열 의 모든 url 가 져 오기 본문

개발 스크랩 메모/PHP

php 로 문자열 의 모든 url 가 져 오기

렉사이 2020. 11. 29. 22:41

나 는 한 텍스트 문자열 에서 url 배열 을 얻 을 방법 을 생각 하고 있다.

텍스트 의 형식 은 다음 과 같 습 니 다:

위 에 무 작위 적 인 글 이 있 습 니 다.

http: / techcrunch. com, 2012 / 07 / 20 / kickstarter - fllashr - wants - tone - make - the - iphones - bezel - a - maassive - notification - light /?grcc = 888888 z0 zwdgtz0 z0 z0 & grcc2 = 835637c 33f965 e6 cdd34c 87219233711 ~ 134428462249 ~ fca4 faf1286 d8a77f26f263fdeed 202 ~ 510 f3724b50a 5 e91f955fac3 fa ~ 427416490 ~ 0 ~ 0 ~ 0 ~ 0 ~ 0 ~ 0 ~ 0 ~ 7 ~ 3 ~ 3http: / techcrunch. com, 2012 / 07 / 20 / last - day - top - urchasee - extra - early - bird - tickets - for - disrupt - s /

분명 한 것 은 이 링크 들 은 모든 것 이 될 수 있 습 니 다.

(많은 링크 가 있 습 니 다.

이것 은 단지 제 가 현재 테스트 하고 있 는 링크 일 뿐 입 니 다.

)내 가 만약 에 내 regex 같은 간단 한 URL 을 사용한다 면, 일이 정상 적 입 니 다.

사용 중:

preg_match_all('((https?|ftp|gopher|telnet|file|notes|ms-help):'. '((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)', $bodyMessage, $matches, PREG_PATTERN_ORDER); 

내 가 인쇄 할 때 (일치), 얻 은 결 과 는:

Array ( [0] => Array ( [0] => http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphon= [1] => http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tick= [2] => http://techcrunch.co= [3] => http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-ip= [4] => http://techcrunch.com/2012/07/20/last-day-to-purc= [5] => http://tec= ) ... 

이 배열 의 이 항목 들 은 위 에 연 결 된 완전한 링크 가 아니다.

제 가 원 하 는 게 뭔 지 아 시 는 분?PHP 의 링크 를 얻 기 위해 정규 표현 식 을 찾 았 지만 효과 적 인 것 은 하나 도 없습니다.

감사합니다.

편집:

좋 습 니 다.

메 일 에서 이 링크 들 을 추출 하 겠 습 니 다.

스 크 립 트 에서 메 일 을 처리 하고 메시지 본문 을 캡 처 한 다음 링크 를 잡 으 려 고 합 니 다.

이메일 을 조사해 보 니 왠 지 url 사이 에 빈 칸 을 추가 한 것 같다.

다음은 내 PHP 스 크 립 트 가 본 바디 메시지 의 출력 이다.

 --00248c711bb99ca36d04c54ba5c6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphon= es-bezel-a-massive-notification-light/?grcc=3D88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2= =3D835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033f= deed202~510f37324b14c50a5e9121f955fac3fa~1342747216490~0~0~0~0~0~0~0~0~7~3~ http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tick= ets-for-disrupt-sf/ --00248c711bb99ca36d04c54ba5c6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 

무슨 건의 가 있 으 면, 어떻게 그것 으로 하여 금 인터넷 주 소 를 깨 뜨리 지 않 게 합 니까?

편집

로 렌 트 의 조언 에 따라 다음 코드 를 실행 했다.

 $bodyMessage = str_replace("= ", "",$bodyMessage); 

그러나 내 가 메아리 칠 때, 그것 은 바 꾸 기 가 싫 은 것 같다.

"="

 --00248c711bb99ca36d04c54ba5c6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable http://techcrunch.com/2012/07/20/kickstarter-flashr-wants-to-make-the-iphon= es-bezel-a-massive-notification-light/?grcc=3D88888Z0ZwdgtZ0Z0Z0Z0Z0&grcc2= =3D835637c33f965e6cdd34c87219233711~1342828462249~fca4fa8af1286d8a77f26033f= deed202~510f37324b14c50a5e9121f955fac3fa~1342747216490~0~0~0~0~0~0~0~0~7~3~ http://techcrunch.com/2012/07/20/last-day-to-purchase-extra-early-bird-tick= ets-for-disrupt-sf/ --00248c711bb99ca36d04c54ba5c6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 

대답 하 다.

 /** * * @get URLs from string (string maybe a url) * * @param string $string * @return array * */ function getUrls($string) { $regex = '/https?\:\/\/[^\" ]+/i'; preg_match_all($regex, $string, $matches); //return (array_reverse($matches[0])); return ($matches[0]); } 
Comments