【问题标题】:Appending to string each time a substring is found每次找到子字符串时附加到字符串
【发布时间】:2016-05-29 14:45:31
【问题描述】:

我有一个由 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 附加到其开头。

我该怎么做?

【问题讨论】:

  • 使用explode() 获取一个数组,其中每个url 都是一个数组元素。然后你可以方便地为每个元素附加一些东西。

标签: php string substring


【解决方案1】:
$srcset = str_replace('/wp-content/', 'http://example.com/wp-content/', $srcset);

【讨论】:

  • 哇。我怎么不知道呢?无需循环或任何东西。
猜你喜欢
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-29
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 2012-10-04
相关资源
最近更新 更多