I thought this would be easy, but turning out to be hard. All I am trying to do is replace a value in an object then print out the changed string. This is within Joomla, by the way. Not that it matters, just to explain all the JHTML/and JURi stuff in the code.
The code that I have been trying is…
<?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;
?>
So my str_replace line is where I’m having the problems. $this->containers is just an array of states and other stuff echoes out a dropdown box. I tried to do the replace before it echoes out on the last line, but the words “Your Favorite Places” are still there. Do I have to put this in a foreach loop or something similar?
Here is a partial print_r (in fact the string I want to replace is in it. Category Title => 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
)
Property
$this->containersis an array of objects.You’ll need to iterate through this array, access each object’s title property, and replace that property’s string value (if it is the correct string).
So…
Get rid of this block:
Replace it with this line:
Edit: I’ve added a way to check for part of the string rather than the whole string, and to replace the partial string match in order to preserve whitespaces.