| | |
Str_Replace / Preg_replace
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
What it is doing is its a horizontal nav bar, but whatever page you are on, is the opposite color, Black.
Ideas?
Thanks
navigation (nav.inc.php). So I'll show you my code.
PHP Syntax (Toggle Plain Text)
$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.
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.
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.
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.
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.
•
•
Join Date: Oct 2007
Posts: 11
Reputation:
Solved Threads: 0
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.
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.
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.
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.
![]() |
Similar Threads
- Replacing 2 characters with with preg_replace (PHP)
- get rid of blank lines with strreplace() (PHP)
- vBulletin mod_rewrite for vB 3.0.7 (Existing Scripts)
- Invision Furl problem (PHP)
- Form not sending email (PHP)
- This ought to be simple - extra spaces (PHP)
Other Threads in the PHP Forum
- Previous Thread: How to save javascript variable in php variable??
- Next Thread: Replace a space with an underscore SOLVED
| Thread Tools | Search this Thread |
advanced apache api array beginner binary broken cakephp check checkbox class cms code cookies cron curl database date datepart display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google head href htaccess html if...loop image include includingmysecondfileinthechain insert ip javascript job joomla jquery key library limit link login mail menu mlm multiple mysql oop password paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search server sessions smarty sms sorting source space sql startup stored syntax system table traffic tutorial unicode update upload url validator variable video web youtube zend






