我在数据库中有列内容,例如:
<table border="0" cellspacing="1" cellpadding="1" width="200" align="center">
<tbody>
<tr>
<td>
<a href="http://example.cz/cz/katalog/some_product/">
<img src="http://example.cz/article/3584/photo/some_product.jpg" />
</a>
</td>
<td>
<a href="http://example.cz/cz/katalog/some_product2/">
<img src="http://example.cz/article/3584/photo/some_product2.jpg" />
</a>
</td>
...
如果我做一个mysql查询来获得这些内容,我需要将图片和href标签的来源更改为另一个网站(路径相同),例如:
<table border="0" cellspacing="1" cellpadding="1" width="200" align="center">
<tbody>
<tr>
<td>
<a href="http://anotherWebsite.cz/cz/katalog/some_product/">
<img src="http://anotherWebsite.cz/article/3584/photo/some_product.jpg" />
</a>
</td>
<td>
<a href="http://anotherWebsite.cz/cz/katalog/some_product2/">
<img src="http://anotherWebsite.cz/article/3584/photo/some_product2.jpg" />
</a>
</td>
...
我尝试了功能:
str_replace('example', 'anotherWebsite', $content);
但没有结果。有其他方法可以替换它吗?例如使用regex?如果regex是一种方式,你能告诉我怎么做吗?因为我不擅长正则表达式。真的很感谢。
根据常见条件运行str_replace可能会产生意外的结果。你可以更具体地说
$content = str_replace(array('href="http://example.cz/','src="http://example.cz/'),
array('href="http://anotherWebsite.cz/','src="http://anotherWebsite.cz/'),
$content);
这样,它只替换属于href属性src的值。
您可能还需要为"vs"添加规则。
str_replace
应该可以工作。您确定已将新值分配给变量吗?
$content = str_replace('example', 'anotherWebsite', $content);