每次找到子字符串时追加到字符串

时间:2016-05-29 14:45:31

标签: php string substring

我有一个由html srcset组成的字符串。我使用DOMDocument提取了srcset的值,它类似于:

$srcset = "/wp-content/uploads/2016/05/something-970x647-c-300x190.jpg 300w, 
/wp-content/uploads/2016/05/something-970x647-c-120x76.jpg 120w, 
/wp-content/uploads/2016/05/something-970x647-c-265x168.jpg 265w";

如您所见,网址是相对的。我希望将它们更改为绝对形式,主机类似于http://example.com

为此,我考虑在字符串中循环出现/wp-content并将主机url追加到它的开头。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

$srcset = str_replace('/wp-content/', 'http://example.com/wp-content/', $srcset);