Page 1 of 1

PHP code no longer works: preg_replace

Posted: Sat Apr 10, 2010 2:32 pm
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!

Re: PHP code no longer works: preg_replace

Posted: Sat Apr 10, 2010 4:06 pm
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).

Re: PHP code no longer works: preg_replace

Posted: Sun Apr 11, 2010 5:38 am
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>

Re: PHP code no longer works: preg_replace

Posted: Sun Apr 11, 2010 6:19 am
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.

Re: PHP code no longer works: preg_replace

Posted: Sun Apr 11, 2010 7:08 am
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);

Re: PHP code no longer works: preg_replace

Posted: Sun Apr 11, 2010 3:34 pm
by requinix
No, it's not the same. I gave you a link - check it out.

Re: PHP code no longer works: preg_replace

Posted: Sun Apr 11, 2010 4:26 pm
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?