2013-10-09 74 views
0

我有一个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的文本2。 请帮助我。

+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