justjoe
justjoe

Reputation: 5564

Problem converting path string from "/", "\"

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 
} 

Upvotes: 2

Views: 722

Answers (4)

Levi Hackwith
Levi Hackwith

Reputation: 9332

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

Upvotes: 1

Your Common Sense
Your Common Sense

Reputation: 157828

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

Upvotes: 1

Thiago Belem
Thiago Belem

Reputation: 7832

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

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

Upvotes: 7

codaddict
codaddict

Reputation: 454940

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 .

Upvotes: 1

Related Questions