我认为这很容易,但事实证明这很难。我想要做的就是替换对象中的值然后打印出更改后的字符串。顺便说一句,这是在Joomla内。并不重要,只是解释代码中的所有JHTML /和JURi内容。
我一直在尝试的代码是......
<?php
// Display the child select box.
if (isset($this->containers) && count($this->containers)):
$items = str_replace("Your Favorite Places", "Browse By Region", $this->containers);
echo JHtml::_('select.genericlist', $items, 'finder-containers','onchange="document.location=this.value; return false;"', 'link', 'indented', JUri::getInstance()->toString(array('path')));
endif;
?>
所以我的str_replace
行是我遇到问题的地方。 $this->containers
只是一系列状态,其他东西回显了一个下拉框。我试着在最后一行回音之前做替换,但是“你最喜欢的地方”这个词仍然存在。我是否必须将其置于foreach
循环或类似的东西中?
这是一个部分print_r(实际上我要替换的字符串在其中.Category Title =&gt; Your Favorite Places)
Array (
[0] => stdClass Object (
[category_id] => 1
[title] => Gallery
[alias] => gallery
[slug] => 1:gallery
[level] => 0
[my_items] => 0
[url] => index.php?option=com_gallery&view=images&category_id=1
[route] => index.php?option=com_gallery&view=images&category_id=1:gallery&Itemid=1766
[link] => /your-favorite-places/categories/gallery.html
[indented] => Gallery
)
[1] => stdClass Object (
[category_id] => 164
[title] => Your Favorite Places
[alias] => your-favorite-places
[slug] => 164:gallery/your-favorite-places
[level] => 1
[my_items] => 0
[url] => index.php?option=com_gallery&view=images&category_id=164
[route] => index.php?option=com_gallery&view=images&category_id=164:gallery/your-favorite-places&Itemid=3711
[link] => /your-favorite-places/gallery.html
[indented] => Your Favorite Places
)
[2] => stdClass Object (
[category_id] => 87
[title] => North America
[alias] => north-america
[slug] => 87:gallery/your-favorite-places/north-america
[level] => 2
[my_items] => 0
[url] => index.php?option=com_gallery&view=images&category_id=87
[route] => index.php?option=com_gallery&view=images&category_id=87:gallery/your-favorite-places/north-america&Itemid=1775
[link] => /your-favorite-places/north-america.html
[indented] => North America
)
答案 0 :(得分:1)
属性$this->containers
是一个对象数组。
您需要遍历此数组,访问每个对象的title属性,并替换该属性的字符串值(如果它是正确的字符串)。
因此...
摆脱这个障碍:
<击> 撞击>
<击>$items = str_replace("Your Favorite Places", "Browse By Region", $this->containers);
击> <击> 撞击>
将此替换为此行:
$items = array(); // Create an array to load the objects in as values.
foreach($this->containers as $object) { // Iterate through $this->containers, which is an array. Load the object into temporary variable $object.
if (strpos($object->title, 'Your Favorite Places') !== false) { // If the title property contains a string you're looking for, replace it with the value you want.
$object->title = str_replace('Your Favorite Places', 'Browse By Region', $object->title);
}
if (strpos($object->indented, 'Your Favorite Places') !== false) { // Should work with any property value with whitespaces also.
$object->indented = str_replace('Your Favorite Places', 'Browse By Region', $object->indented);
}
$items[] = $object; // Load the object into array $items.
}
编辑:我添加了一种方法来检查字符串的一部分而不是整个字符串,并替换部分字符串匹配以保留空格。
答案 1 :(得分:0)
如果我猜测,我会说字母“你最喜欢的地方”在$ this-&gt;容器的键中。虽然str_replace()将遍历数组,但它会迭代值,而不是键。
我会尝试类似的事情:
foreach($this->containers as $key => $value) {
if ($key == "Your Favorite Places") {
$items["Browse By Region"] = $value;
} else {
$items[$key] = $value;
}
}
从Stegrex编辑:Changed = to =&gt;在循环中