2011-01-19 5 views
1

가 일부 URL, 11162_sport.html, 11451_sport.html, 11245_sport.html 또는 231sport.html, XXXXX_sport.html 같은 URL이 다음 11162_football.html11451_football.html, 11245_football.html231sport.html은 변화가 없습니다로를 교체 할 때 내가 원하는 있다.PHP 교체 문제

대체 방법, $newurl = preg_replace("_sport.html","_football.html",$url)? 감사.

답변

3

간단히 수행 $newurl = str_replace("_sport.html", "_football.html", $url);
preg_replace() 이상을 수행하는 것보다 빠릅니다.

the manual on str_replace을 참조하십시오. 이 정규 표현식이어야합니다

1

간단한 교체 용으로 str_replace을 사용할 수 있습니다.

1

, 수행

preg_replace('/_sport\.html$/', '_football.html', $url); 

str_replace() 불확실 줄 끝 마커 ($)와 정규 표현식 반면 sport.html모든 발행 수를 대체 할 유일한 대체합니다 URL의 끝에있는 패턴.

도트는 새 문자를 제외한 모든 문자와 일치하므로 이스케이프해야합니다.