替换字符串数组中的所有字符串将不起作用
Replace all strings from an array of strings won't work
我有以下字符串
$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"]"
}
这意味着 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"]
我有以下字符串
$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"]"
}
这意味着 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"]