Str_Replace / Preg_replace

Reply

Join Date: Oct 2007
Posts: 11
Reputation: jtmcgee is an unknown quantity at this point 
Solved Threads: 0
jtmcgee jtmcgee is offline Offline
Newbie Poster

Str_Replace / Preg_replace

 
0
  #1
Dec 27th, 2008
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,426
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 230
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Str_Replace / Preg_replace

 
0
  #2
Dec 27th, 2008
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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: jtmcgee is an unknown quantity at this point 
Solved Threads: 0
jtmcgee jtmcgee is offline Offline
Newbie Poster

Re: Str_Replace / Preg_replace

 
0
  #3
Dec 27th, 2008
Originally Posted by ShawnCplus View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,426
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 230
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Str_Replace / Preg_replace

 
0
  #4
Dec 27th, 2008
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.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: jtmcgee is an unknown quantity at this point 
Solved Threads: 0
jtmcgee jtmcgee is offline Offline
Newbie Poster

Re: Str_Replace / Preg_replace

 
0
  #5
Dec 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,426
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 230
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Str_Replace / Preg_replace

 
0
  #6
Dec 28th, 2008
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
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 11
Reputation: jtmcgee is an unknown quantity at this point 
Solved Threads: 0
jtmcgee jtmcgee is offline Offline
Newbie Poster

Re: Str_Replace / Preg_replace

 
0
  #7
Dec 28th, 2008
Manually? It changes sometimes Pages is black, sometimes Blog is, it all depends on the page including it.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC