azukah Posted June 14, 2011 Share Posted June 14, 2011 i have this code to change $greeting based on the URL. if the URL contains ../es or ../it, $greeting will change to the appropriate language greeting. <?php switch( dirname( $_SERVER['PHP_SELF'])) { case '/es': $greeting = 'Hola!'; break; case '/it': $greeting = 'Ciao!'; break; default: $greeting = 'Hello!'; break; } ?> //here's the actual line where I call $greeting <li><a class="home" href="index.php"><?php echo $greeting; ?></a></li> the code shows "Hello!" which is the default only. regardless of the folder I am in (".../es" or ".../it").. ay ideas?? Link to comment https://forums.phpfreaks.com/topic/239302-help-with-switch-case-when-url-contains/ Share on other sites More sharing options...
jcbones Posted June 14, 2011 Share Posted June 14, 2011 Try using: (one should work, depending on server type). switch(strrchr( __DIR__ ,'\\')) { //or switch(strrchr(__DIR__,'/')) { Link to comment https://forums.phpfreaks.com/topic/239302-help-with-switch-case-when-url-contains/#findComment-1229365 Share on other sites More sharing options...
azukah Posted June 14, 2011 Author Share Posted June 14, 2011 @jcbones - I tried both ways and didn't work Link to comment https://forums.phpfreaks.com/topic/239302-help-with-switch-case-when-url-contains/#findComment-1229366 Share on other sites More sharing options...
jcbones Posted June 14, 2011 Share Posted June 14, 2011 Did you echo it, to see what it returns? echo '1: ' . strrchr( __DIR__ ,'\\') . '<br />'; echo '2: ' . strrchr( __DIR__ ,'/') . '<br />'; Link to comment https://forums.phpfreaks.com/topic/239302-help-with-switch-case-when-url-contains/#findComment-1229728 Share on other sites More sharing options...
redixx Posted June 14, 2011 Share Posted June 14, 2011 $_SERVER['PHP_SELF'] doesn't point to a directory, it points to the current file. How is your URL going to be setup? Like this: example.com/es/index.php Or like this: example.com/index.php/es If the first, use .htaccess. If the second, try this: $segments = exp('/', $_SERVER['PHP_SELF']); switch($segments[1]) { case 'es': $greeting = 'Hola!'; break; case 'it': $greeting = 'Ciao!'; break; default: $greeting = 'Hello!'; break; } Link to comment https://forums.phpfreaks.com/topic/239302-help-with-switch-case-when-url-contains/#findComment-1229734 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.