PHP code no longer works: preg_replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

PHP code no longer works: preg_replace

Post by loweauto »

Hello, wasn't sure where to post this - so feel free to move it accordingly.

I'm having problems with some PHP code that I implemented on a site. It worked when I handed everything over, but after a few months - probably PHP updates, it stopped working.

In more detail:

It is a bilingual site, and has duplicate HTML files for both languages, e.g.:

.../eng/index.html
.../deu/index.html

To switch between languages, I implemented the following code (this example is from what appears on the EN version):

Code: Select all

<div class="loweAutomobil" id="language">English | <?php $myUrl = "http://".$SERVER_NAME.$REQUEST_URI; $alternativeUrl = preg_replace("/eng/","deu",$myUrl); echo "<a href= ".$alternativeUrl.">Deutsch</a>"; ?></div>
For some reason, it doesn't work anymore as it should (i.e. display "Deutsch" as a link with the current 'eng' in the URL replaced by 'deu').

Instead it goes to: http:localhost/

Does anyone have any ideas as to why? How can I fix it?

You can view the site here: http://loeweautomobil.de/eng/index.html


Thank you for your time!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP code no longer works: preg_replace

Post by requinix »

register_globals got turned off.

Instead of using $SERVER_NAME and $REQUEST_URI you need to use $_SERVER["SERVER_NAME"] and $_SERVER["REQUEST_URI"] respectively.

In general, $UPPERCASE variables that don't work are $_SERVER["UPPERCASE"] now, and input needs to be accessed via $_GET (from the URL) and $_POST (from a POSTed form).
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code no longer works: preg_replace

Post by loweauto »

tasairis wrote:register_globals got turned off.

Instead of using $SERVER_NAME and $REQUEST_URI you need to use $_SERVER["SERVER_NAME"] and $_SERVER["REQUEST_URI"] respectively.

In general, $UPPERCASE variables that don't work are $_SERVER["UPPERCASE"] now, and input needs to be accessed via $_GET (from the URL) and $_POST (from a POSTed form).
thanks!

so it should be like this?

Code: Select all

<div class="loweAutomobil" id="language"> 
English | 
<?php 
$myUrl = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"]; 
$alternativeUrl = preg_replace("/eng/", "deu", $myUrl); 
echo "<a href= ". $alternativeUrl .">Deutsch</a>"; 
?> 
</div>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP code no longer works: preg_replace

Post by requinix »

Oh, I forgot to mention:

preg_replace is for when you want to do complicated replaces. You don't.
Use str_replace instead.
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code no longer works: preg_replace

Post by loweauto »

tasairis wrote:Oh, I forgot to mention:

preg_replace is for when you want to do complicated replaces. You don't.
Use str_replace instead.

thanks!! would it be the same format for my case? e.g.:

Code: Select all

str_replace("/eng/", "deu", $myUrl);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP code no longer works: preg_replace

Post by requinix »

No, it's not the same. I gave you a link - check it out.
loweauto
Forum Newbie
Posts: 20
Joined: Sat Apr 10, 2010 2:23 pm

Re: PHP code no longer works: preg_replace

Post by loweauto »

tasairis wrote:No, it's not the same. I gave you a link - check it out.
i did,
so it would be:

str_replace("eng", "deu", $myUrl);

right?
Post Reply