【问题标题】:Replace all strings from an array of strings won't work替换字符串数组中的所有字符串不起作用
【发布时间】:2019-09-18 12:32:59
【问题描述】:

我有以下字符串

$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)?我该如何解决这个问题?

【问题讨论】:

  • var_dump($contentID); 启蒙……
  • 我真的不明白你的目标是什么。
  • 我不会拆分,而是使用匹配 - preg_match_all('/\[.*\]/U', $content, $contentID); $contentID = $contentID[0]; 然后您可以保持其余代码不变,并且它不会像答案中建议的解决方法那样容易失败。
  • @04FS 再次感谢您的建议,我可能不得不将问题提交到另一个论坛,因为 content="HERE" 的值也可能有嵌套标签,但我会尝试将整个功能更改为避免不必要的并发症。

标签: php arrays foreach str-replace


【解决方案1】:

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"]"
}

这意味着 foreach 上的 str_replace('[tab', '[tab id="'.$id.'-'.$i.'"', $tabID) 不起作用,因为对于数组索引 (1, 2),没有要替换的字符串 [tab

一种解决方法是使用strpos 函数检查[tab 是否存在,如下所示:

$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"]

【讨论】:

  • 啊,你说的太对了,非常感谢你发现了缺失的[
  • 如果不以 [tab 开头的部分之一应在任何地方以小写形式包含 tab...
  • @04FS 我想,在content="HERE" 中还有tab 这个词可能会搞砸很多事情,所以我正在考虑用] 拆分字符串并读取@987654335 @ 在每个部分。
  • @thednp 这可能是一个更好的方法
猜你喜欢
  • 2012-08-01
  • 2015-06-24
  • 2016-11-08
  • 2021-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
相关资源
最近更新 更多