1

我以为这很容易,但结果很难。我要做的就是替换对象中的一个值,然后打印出更改后的字符串。顺便说一句,这是在 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(实际上我要替换的字符串就在其中。类别标题=>您最喜欢的地方)

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
    )
4

2 回答 2

2

属性$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.
}

编辑:我添加了一种方法来检查部分字符串而不是整个字符串,并替换部分字符串匹配以保留空格。

于 2012-08-10T00:36:52.597 回答
0

如果我猜的话,我会说字符串“Your Favorite Places”在 $this->containers 的键中。虽然 str_replace() 将迭代数组,但它迭代的是值,而不是键。

我会尝试类似的东西:

foreach($this->containers as $key => $value) {
   if ($key == "Your Favorite Places") {
       $items["Browse By Region"] = $value;
   } else {
       $items[$key] = $value;
   }
 }

从 Stegrex 编辑:在循环中将 = 更改为 =>

于 2012-08-09T22:50:20.550 回答