将PHP字符串函数转换为Javascript / jQuery

时间:2011-10-05 04:04:48

标签: php jquery substr strpos

这是我试图转换为Javascript / jQuery的PHP代码。有什么想法吗?

$root_folder = 'es';
$url = "http://domainNamehere.com/event/test-event-in-the-future/";

$p = strpos($url, '/', 8);
$url_new = sprintf('%s/%s/%s', substr($url, 0, $p), $root_folder, substr($url, $p+1));

3 个答案:

答案 0 :(得分:0)

strpos() ~ .indexOf()
substr() ~ .substring()
sprintf() ~ +

答案 1 :(得分:0)

var root_folder = "es",
    url = "http://domainNamehere.com/event/test-event-in-the-future/",
    url_new = "",
    path_finder = /^(((http)s?:)?\/\/[^\/]+\/)/i;

url_new = url.replace(path_finder, "$1" + root_folder + "/");

答案 2 :(得分:0)

PHP.js为JavaScript提供了许多常见PHP函数的端口。