문제

PHP에서 preg_replace 함수를 사용하고 사용자가 비트 단축 링크로 제출하는 URL을 대체하려고 시도합니다.

$comment = preg_replace( '/(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/i', '', $strText );
.

이렇게하면 주석이 표시되고 URL을 "닦으십시오"합니다.그 질문은 텍스트에서 URL을 가져 와서 나중에 추가 할 수 있습니까?

도움이 되었습니까?

해결책

PREG_REPLACE_CALLBACK ()

php.net의 예 :

<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
  // as usual: $matches[0] is the complete match
  // $matches[1] the match for the first subpattern
  // enclosed in '(...)' and so on
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(\d{2}/\d{2}/)(\d{4})|",
            "next_year",
            $text);

?>
.

URL을 대체하는 콜백 함수를 정의합니다.매개 변수와 일치 항목을 수신하며 해당 경기에 따라 대체 문자열에 따라 형식을 지정합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top