I am having problems with str_replace. What I am doing for the administration pages of my CMS I have a include page for the top
navigation (nav.inc.php). So I'll show you my code.

$page = $_SERVER['SCRIPT_NAME'];
	$prefix = '/administration/';
	
	//get main and sub
	$main = '<div class="main-links"><a class="wnav" href="'.$prefix.'">Home</a><a class="wnav" href="'.$prefix.'links.php">Links</a><a class="wnav" href="'.$prefix.'manage.php">Posts</a><a class="wnav" href="'.$prefix.'pages.php">Pages</a><a class="wnav" href="'.$prefix.'media.php">Media</a><a class="wnav" href="'.$prefix.'options.php">Options</a><a class="wnav" href="'.$prefix.'users.php">Users</a><a class="wnav" href="'.$prefix.'login.php?logout">Logout</a></div>';
        
        $search = "<a class='wnav' href='".$page."'>";
        $replace = "<a class='bnav' href='".$page."'>";
	$main = str_replace($search, $replace, $main);

What it is doing is its a horizontal nav bar, but whatever page you are on, is the opposite color, Black.

Ideas?
Thanks

Why are you doing a string replace after you create the string instead of actually modifying the original string, its wasted CPU cycles, even if it is minimal.

Why are you doing a string replace after you create the string instead of actually modifying the original string, its wasted CPU cycles, even if it is minimal.

Couple of CPU cycles are irrelevant to me, have any ideas on the code problem though?

Do some debugging to make sure $_SERVER is the string you're looking for, if the string isn't being replaced correctly its most likely because you are searching for the wrong thing. As a side note, the replace is really, really unnecessary unless there is another reason for it. It would be faster just to fix the strings.

What do you mean by fix the strings, isn't this the only way to do this? Or only simple way, keep it relativley simple, as I have been only coding for 4 years, and just turned 14.

Also I did the "debugging" and the $_SERVER is giving me what I want /administration/____.php.

I mean fix the string. You have a string that is essentially static, then you're doing a replace on it for no reason. Just manually replace wnav with bnav

Manually? It changes sometimes Pages is black, sometimes Blog is, it all depends on the page including it.

This article has been dead for over six months. Start a new discussion instead.