2013-10-09 4 views
0

변수에 put 된 html 코드가 있습니다. str_replace를 사용하여 상대 이미지 src를 절대 aby로 바꾸려고합니다. 이렇게 :PHP str_replace가 작동하지 않습니다.

$export = 'some html codes' 
$repthis = 'src="/images'; 
$reptothis = 'src="http://images.site.com'; 
$export = str_replace($repthis, $reptothis, $export); 

이 코드는 나를 위해 작동하지 않습니다. 테스트를 위해이 코드를 시도했지만 작동 중입니다.

$export = 'some html codes' 
$repthis = "text1"; 
$reptothis = "text2"; 
$export = str_replace($repthis, $reptothis, $export); 

이 코드는 HTML 코드에서 text1을 text2로 올바르게 대체합니다. 도와주세요.

+0

코드는 올바른 보인다. 패턴을 다시 확인하십시오. 원본 텍스트에 정확히'src = "/ images'가 있습니까? – WebNovice

답변

0

방법을 잘못 아무것도 할 수없는 것 하고있다.
입력 데이터, 검색 문자열 및 대체 문자열을 다시 한 번 확인하면됩니다.

$inputString = '<img src="/images/logo.jpg" />'; 

$searchString = 'src="/images'; 

$replacementString = 'src="http://images.site.com'; 

echo str_replace($searchString ,$replacementString ,$inputString); 

표시 :

<img src="http://images.site.com/logo.jpg" /> 
0

코드가 정확히 완벽합니다. 코드를 다시 확인하거나 오류를 추가하십시오.

당신이 src="/images'

$export = 'some html codes : src="/images'; 
          ^^^^^^^^^^^^^^^^ 
$repthis = 'src="/images'; 
$reptothis = 'src="http://images.site.com'; 
$export = str_replace($repthis, $reptothis, $export); 
echo $export; 


출력을 문장의 끝에 세미콜론 (;를) 누락 누락 될 수 있습니다

some html codes : src="http://images.site.com 
관련 문제