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状態の配列であり、他のものがドロップダウンボックスにエコーアウトします。最後の行にエコーする前に置換を試みましたが、「YourFavoritePlaces」という言葉がまだ残っています。これをループなどに入れる必要があり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

推測すると、$this->containersのキーには「YourFavoritePlaces」という文字列が含まれていると思います。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 に答える