php - 替换字符串数组中的所有字符串不起作用
问题描述
我有以下字符串
$content = '[tab title="Tab A" content="Tab a content."][tab title="Tab B" content="Tab B content"][tab title="Tab C" content="Tab C content"]';
现在我想像这样拆分内容:
$contentID = preg_split('/(\]\[)|(\]\s*\[)/', $content);
到目前为止一切顺利,现在我需要检查$contentID
数组的每个字符串是否包含该id=
部分,以便在缺少时添加它:
// set a new array
$newContentID = array();
// set a unique ID
$id = 'unique';
// include an id attribute for each string part
foreach ( $contentID as $i => $tabID ) {
$newContentID[$i] = !strpos( $tabID, 'id=' ) === true ? str_replace( '[tab', '[tab id="'.$id.'-'.$i.'"', $tabID) : $tabID;
}
最后,我只是将所有内容内爆到同一个$content
数组中。
$content = implode('][',$newContentID);
新的内容是#content
这样的:
var_dump($content);
/////////////// RESULT ///////////////////
string "[tab id="unique-0" title="Tab A" content="Tab a content."][tab title="Tab B" content="Tab B content"][tab title="Tab C" content="Tab C content"]"
var_dump($contentID);
/////////////// RESULT ///////////////////
array(3) {
[0]=>
string(13) "
[tab id="tab-a" title="Tab A" active="1" content="Tab a content""
[1]=>
string(13) "tab id="tab-b" title="Tab B" content="Tab B content""
[2]=>
string(13) "tab id="tab-c" title="Tab C" content="Tab C content."]
"
}
为什么不做foreach
我期望它做的事情(id
在缺少的每个字符串部分中添加)?我该如何解决这个问题?
解决方案
id 部分没有被添加,因为结果preg_split
是:
array(3) {
[0]=>
string(43) "[tab title="Tab A" content="Tab a content.""
[1]=>
string(41) "tab title="Tab B" content="Tab B content""
[2]=>
string(42) "tab title="Tab C" content="Tab C content"]"
}
这意味着str_replace('[tab', '[tab id="'.$id.'-'.$i.'"', $tabID)
foreach 上的 将不起作用,因为对于数组索引 (1, 2) 没有[tab
要替换的字符串。
一种解决方法是使用如下函数检查是否[tab
存在strpos
:
$content = '[tab title="Tab A" content="Tab a content."][tab title="Tab B" content="Tab B content"][tab title="Tab C" content="Tab C content"]';
$contentID = preg_split('/(\]\[)|(\]\s*\[)/', $content);
// set a new array
$newContentID = array();
// set a unique ID
$id = 'unique';
// include an id attribute for each string part
foreach ( $contentID as $i => $tabID ) {
$newContentID[$i] = strpos($tabID, 'id=') === false ? addId($tabID, $id, $i) : $tabID;
}
$content = implode('][',$newContentID);
function addId($tabID, $id, $i) {
if (strpos($tabID, '[tab') === 0) {
return str_replace('[tab', '[tab id="'.$id.'-'.$i.'"', $tabID);
} else if (strpos($tabID, 'tab') === 0) {
return 'tab id="' . $id . '-' . $i . '"' . substr($tabID, 3);
}
}
结果echo $content
:
[tab id="unique-0" title="Tab A" content="Tab a content."][tab id="unique-1" title="Tab B" content="Tab B content"][tab id="unique-2" title="Tab C" content="Tab C content"]
推荐阅读
- excel - 用 Cribbage 手数 15
- c++ - 使用 Array 的结构在堆中创建动态数组
- elasticsearch - 是否可以只从 Elasticsearch 中检索 doc id 和 score 而无需执行搜索的 fetch 阶段?
- javascript - JS/TS Promises - 解析(对象)数组,但解析时为空
- python - VSCode:如何在每次使用 VS Code 任务运行之前自动清除 Python 终端输出窗口?
- reactjs - 在 componentDidMount for 循环中向数组中添加多行
- scala - 从标准 Akka Actor 迁移到具有背压和节流的 Akka Streams
- mongodb - Sitecore 8.1 MongoDB 集合未更新
- python - 尽管标头从那里开始,但 CORS 标头“Access-Control-Allow-Origin”丢失
- log4net - 当应用程序作为服务运行时,Log4net RollingFileAppender 不写入