Adding extra strstr step to an existing set of operations ...

Hi guys.

I’m trying to add an additional functional step to a bunch of pre-existing PHP code, but getting tangled up with all the syntax of the stuff surrounding the bit I need to operate on.

Basically it’s part of an operation that takes a chain of Joomla CMS menu sub-item names and turns it into a chunk of text that can be used for navigation “breadcrumbs”. This text just ends up as a bunch of elements inserted into the output HTML.

Unfortunately another Joomla menu module creates menu item names with “|” characters and some following text, which I want to chop out.

Text the module outputs : “Some Menu Title||and some extra text”
What I want to end up with : “Some Menu Title”

First, the existing code :-
[php]public static function getList(&$params)
{
// Get the PathWay object from the application
$app = JFactory::getApplication();
$pathway = $app->getPathway();
$items = $pathway->getPathWay();

	$count = count($items);
	// don't use $items here as it references JPathway properties directly
	$crumbs	= array();
	for ($i = 0; $i < $count; $i ++)
	{
		$crumbs[$i] = new stdClass();
		$crumbs[$i]->name = stripslashes(htmlspecialchars($items[$i]->name, ENT_COMPAT, 'UTF-8'));
		$crumbs[$i]->link = JRoute::_($items[$i]->link);
	}

	if ($params->get('showHome', 1))
	{
		$item = new stdClass();
		$item->name = htmlspecialchars($params->get('homeText', JText::_('MOD_BREADCRUMBS_HOME')));
		$item->link = JRoute::_('index.php?Itemid='.$app->getMenu()->getDefault()->id);
		array_unshift($crumbs, $item);
	}

	return $crumbs;[/php]

The only really relevant bit is the data contained in the property name - that’s what I want to chop, after htmlspecialchars etc has done it’s stuff.
We can ignore the stuff in the “if($params->get(‘showHome’, 1))” conditional - nothing that part gets to operate on is going to have the surplus |some extra text addition.

I’ve found an example with strstr that seems to do just what I want … take the data in $string and truncate it after character |

Example code :-
[php]$string = strstr( $string, ‘|’, true);[/php]

So … the question:- how to include/append that strstr operation to
$crumbs[$i]->name = stripslashes(htmlspecialchars($items[$i]->name, ENT_COMPAT, ‘UTF-8’));

Any help gratefully received.

kurai

Never mind … solved it myself :slight_smile:

Instead of trying to unpick all the (presumably deeply neccessary) black magic stuff I just applied the strstr to the end-stage echo much later in the code, where things are a lot less busy.

i.e.
[php]echo ‘’ . $item->name . ‘’;

becomes:-

echo ‘’ . $item->name = strstr( $item->name, ‘|’, true) . ‘’;[/php]

Update: strstr caused other headaches because, I’m guessing, it acted on all members of the array instead of just the one needed at that point.

explode did the job more cleanly :-

[php]$parts = explode("|",$item->name); //break the string up around the “|” character in $item->name
$item->name = $parts[‘0’]; //grab the first part, ignore the rest

echo ‘’ . $item->name . ‘’;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service