[ create a new paste ] login | about

Link: http://codepad.org/hzpWmpzk    [ raw code | output | fork ]

PHP, pasted on Oct 13:
<?php

    function startsWith($haystack, $needle) {
        // search backwards starting from haystack length characters from the end
        return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
    }

    function join_paths() {
    /* Ricardo Galli, http://stackoverflow.com/a/15575293/2375207 */
    $paths = array();

    foreach (func_get_args() as $arg) {
        if ($arg !== '') { $paths[] = $arg; }
    }

    return preg_replace('#/+#','/',join('/', $paths));
}


    function join_paths_2() {
        /* source http://stackoverflow.com/a/15575293/2375207 */
        $paths = array();
        $answer = "";
        foreach (func_get_args() as $arg) {
            if ($arg !== '')
            {
                if(startsWith($arg, "http:"))
                {
                    $arg = str_replace("http://","",$arg);
                    $answer.= "http://";
                } elseif(startsWith($arg, "https:"))
                {
                    $arg = str_replace("https://","",$arg);
                    $answer.= "https://";
                }
                $paths[] = $arg;
            }
        }

        $answer .= preg_replace('#/+#','/',join('/', $paths));
        return $answer;
    }

    echo '"http://domain.de","/a","/b/"';
    echo "\n".join_paths("http://domain.de","/a","/b/");
    echo "\n".join_paths_2("http://domain.de","/a","/b/");


Output:
1
2
3
"http://domain.de","/a","/b/"
http:/domain.de/a/b/
http://domain.de/a/b/


Create a new paste based on this one


Comments: