views:

66

answers:

4

i got this code

$current_path = str_replace('\', '/', getcwd()); //c://xampp/htdoc

Why it fail replace '\' with '/' in directory patch ? why is the reason and how to handle this problem ?

EDIT This code use to return path (or something like that) use with HTML TAG base.



$current_path = getcwd();

function get_basepath() { 
    global $current_path; 

    $current_path  = str_replace('\\', '/', $current_path );                        // C:\xampp\htdocs\php\gettingstarted  

    $cur_root = $_SERVER['HTTP_HOST'];              // localhost
    $cur_docroot = $_SERVER['DOCUMENT_ROOT'];       // C:/xampp/htdocs/
    $cur_filepath = $_SERVER['SCRIPT_FILENAME'];    // C:/xampp/htdocs/php/gettingstarted/index.php 
    $filepath = str_replace($cur_docroot, '', $current_path);

    return "http://$cur_root/" . $filepath . "/";       // http://localhost/php/gettingstarted/index1.php 
} 

+1  A: 

Use \\ in place of \ as:

 $current_path = str_replace('\\', '/', getcwd()); 

\ is used to escape a char with special meaning. Now \ itself is a char with special meaning so escape it with another \ like \\ to get a literal .

codaddict
hi, thanks for the answer. sorry i'm not choosing you
justjoe
+6  A: 

You need to use a double \ since the \ escape the next character. :)

$current_path = str_replace('\\', '/', getcwd());
TiuTalk
yes, i forgot about that. thank you very much
justjoe
+1  A: 

That's PHP string syntax question and you don't need this replacement at all.

Col. Shrapnel
care to explain why i don't need this replacement ?
justjoe
@justjoe care to explain why do you ever need to replace if everything works fine without replacement?
Col. Shrapnel
i want to use html tag base in the page i create. from return value of getcwd(), i got path directory then i will string replace it with my $_SERVER['DOCUMENT_ROOT']...in the end result will besomething like <br/> http:/ /localhost/php/gettingstarted/
justjoe
@Col. Shrapnel : thanks for asking, i have edit my question and put the code i just create.
justjoe
@justjoe eheheh thanks for replying. Are you sure you need such a complex venture to get current filename addrress? `$_SERVER['PHP_SELF']` is probably what you're looking for :)
Col. Shrapnel
OMG! your're right. ;D. col, shrapnel, thank you for you. this is embarrassing. haha
justjoe
@justjoe note that you don't usually need to mention hostname in the HTML links. path from the web root is enough. And can be more reliable as you can leave it unchanged when switching hosts. `<a href="/php/gettingstarted/index1.php">` and `<img src="/img/border.gif">` would work everywhere.
Col. Shrapnel
@col. shrapnel, yes i know about that. the problem when we use non-absolute path (??) without base is when we try to save the page via browser as HTML page. We don't know where it come from cause the link has broken.
justjoe
@justjoe Actually, everything that starts from the `/` **is** absolute path. By any means. Speaking of saving pages, it is not quite usual task and most browsers do add a site base themselves. Anyway, nobody cares of that as of it's disadventades. Just take a look at this page source: there is no hostname links for the current host.
Col. Shrapnel
@col.shrapnel : yes, i believe your're right. i'm thinking to throw away the base support. But what i code now (as my pet project learning php) is some kind reference web. The first web app that i think cool is phpdoc and codex.wordpress.org (codex use base and i want the same support).
justjoe
+1  A: 
$current_path = str_replace('\\', '/', getcwd()); //c://xampp/htdoc
Levi Hackwith