943,923 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 976
  • PHP RSS
Dec 27th, 2008
0

Str_Replace / Preg_replace

Expand Post »
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.

PHP Syntax (Toggle Plain Text)
  1. $page = $_SERVER['SCRIPT_NAME'];
  2. $prefix = '/administration/';
  3.  
  4. //get main and sub
  5. $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>';
  6.  
  7. $search = "<a class='wnav' href='".$page."'>";
  8. $replace = "<a class='bnav' href='".$page."'>";
  9. $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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtmcgee is offline Offline
13 posts
since Oct 2007
Dec 27th, 2008
0

Re: Str_Replace / Preg_replace

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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Dec 27th, 2008
0

Re: Str_Replace / Preg_replace

Click to Expand / Collapse  Quote originally posted by ShawnCplus ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtmcgee is offline Offline
13 posts
since Oct 2007
Dec 27th, 2008
0

Re: Str_Replace / Preg_replace

Do some debugging to make sure $_SERVER['SCRIPT_NAME'] 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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Dec 28th, 2008
0

Re: Str_Replace / Preg_replace

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.
Last edited by jtmcgee; Dec 28th, 2008 at 12:51 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtmcgee is offline Offline
13 posts
since Oct 2007
Dec 28th, 2008
0

Re: Str_Replace / Preg_replace

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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Dec 28th, 2008
0

Re: Str_Replace / Preg_replace

Manually? It changes sometimes Pages is black, sometimes Blog is, it all depends on the page including it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jtmcgee is offline Offline
13 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: How to save javascript variable in php variable??
Next Thread in PHP Forum Timeline: Replace a space with an underscore SOLVED





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC