{"id":209,"date":"2016-08-15T10:58:20","date_gmt":"2016-08-15T02:58:20","guid":{"rendered":"http:\/\/loveuhouse.cn\/wordpress\/?p=209"},"modified":"2016-08-15T11:04:49","modified_gmt":"2016-08-15T03:04:49","slug":"js%e5%ae%9e%e7%8e%b0php%e7%9a%84printf%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/loveuhouse.cn\/wordpress\/?p=209","title":{"rendered":"JS\u5b9e\u73b0php\u7684printf\u65b9\u6cd5"},"content":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"http:\/\/www.jb51.net\/article\/60398.htm\">http:\/\/www.jb51.net\/article\/60398.htm<\/a>\uff08\u6211\u77e5\u9053\u8fd9\u4e0d\u662f\u539f\u4f5c\u8005\uff0c\u8bf7\u77e5\u9053\u7684\u8f6c\u544a\u6211jackqqq123@sina.com\uff09<\/p>\n<blockquote><p>\u7b80\u5355\u7248<\/p><\/blockquote>\n<pre class=\"lang:default decode:true \" title=\"\u3010js\u3011printf\u7b80\u5355\u7248\">\/*\n\nvar hangstr=\"this is {0},{1}\";\nprintf(hangstr,\"me\",\"guy\");\n\/\/result :this is me,guy\n*\/\nfunction printf() {\n  var num = arguments.length;\n  var oStr = arguments[0];\n  for (var i = 1; i &lt; num; i++) {\n    var pattern = \"\\\\{\" + (i-1) + \"\\\\}\";\n    var re = new RegExp(pattern, \"g\");\n    oStr = oStr.replace(re, arguments[i]);\n  }\n  return oStr;\n}<\/pre>\n<blockquote><p>\u5b8c\u6574\u529f\u80fd<\/p><\/blockquote>\n<p>1.%% - \u8fd4\u56de\u767e\u5206\u53f7\u672c\u8eab<br \/>\n2.%b - \u4e8c\u8fdb\u5236\u6570\u5b57<br \/>\n3.%c - ASCII\u5bf9\u5e94\u7684\u5b57\u7b26<br \/>\n4.%d - \u6574\u6570<br \/>\n5.%f - \u6d6e\u70b9\u6570<br \/>\n6.%o - \u516b\u8fdb\u5236\u6570\u5b57<br \/>\n7.%s - \u5b57\u7b26\u4e32<br \/>\n8.%x - 16\u8fdb\u5236\u6570\u5b57 (\u5c0f\u5199\u5b57\u6bcd\u5f62\u5f0f)<br \/>\n9.%X - 16\u8fdb\u5236\u6570\u5b57 (\u5927\u5199\u5b57\u6bcd\u5f62\u5f0f)<\/p>\n<p>\u5728 % \u53f7\u548c\u901a\u914d\u5b57\u7b26\u4e4b\u95f4\u53ef\u7528\u7684\u9009\u9879\u5305\u62ec (\u6bd4\u5982 %.2f)\uff1a<\/p>\n<p>1.+????? (\u5f3a\u5236\u5728\u6570\u5b57\u524d\u9762\u663e\u793a + \u548c - \u7b26\u53f7\u4f5c\u4e3a\u6b63\u8d1f\u6570\u6807\u8bb0\u3002\u7f3a\u7701\u60c5\u51b5\u4e0b\u53ea\u6709\u8d1f\u6570\u624d\u663e\u793a - \u7b26\u53f7)<br \/>\n2.-????? (\u53d8\u91cf\u5de6\u5bf9\u9f50)<br \/>\n3.0????? (\u4f7f\u75280\u4f5c\u4e3a\u53f3\u5bf9\u9f50\u7684\u586b\u5145\u5b57\u7b26)<br \/>\n4.[0-9]? (\u8bbe\u7f6e\u53d8\u91cf\u7684\u6700\u5c0f\u5bbd\u5ea6)<br \/>\n5..[0-9] (\u8bbe\u7f6e\u6d6e\u70b9\u6570\u7cbe\u5ea6\u6216\u5b57\u7b26\u4e32\u7684\u957f\u5ea6)<\/p>\n<pre class=\"lang:default decode:true\">\/*\n*\/\n\/**\n*\n*  Javascript sprintf\n*  http:\/\/www.webtoolkit.info\/\n*\n*\n**\/\nsprintfWrapper = {\n  init : function () {\n    if (typeof arguments == \"undefined\") { return null; }\n    if (arguments.length &lt; 1) { return null; }\n    if (typeof arguments[0] != \"string\") { return null; }\n    if (typeof RegExp == \"undefined\") { return null; }\n    var string = arguments[0];\n    var exp = new RegExp(\/(%([%]|(\\-)?(\\+|\\x20)?(0)?(\\d+)?(\\.(\\d)?)?([bcdfosxX])))\/g);\n    var matches = new Array();\n    var strings = new Array();\n    var convCount = 0;\n    var stringPosStart = 0;\n    var stringPosEnd = 0;\n    var matchPosEnd = 0;\n    var newString = '';\n    var match = null;\n    while (match = exp.exec(string)) {\n      if (match[9]) { convCount += 1; }\n      stringPosStart = matchPosEnd;\n      stringPosEnd = exp.lastIndex - match[0].length;\n      strings[strings.length] = string.substring(stringPosStart, stringPosEnd);\n      matchPosEnd = exp.lastIndex;\n      matches[matches.length] = {\n        match: match[0],\n        left: match[3] ? true : false,\n        sign: match[4] || '',\n        pad: match[5] || ' ',\n        min: match[6] || 0,\n        precision: match[8],\n        code: match[9] || '%',\n        negative: parseInt(arguments[convCount]) &lt; 0 ? true : false,\n        argument: String(arguments[convCount])\n      };\n    }\n    strings[strings.length] = string.substring(matchPosEnd);\n    if (matches.length == 0) { return string; }\n    if ((arguments.length - 1) &lt; convCount) { return null; }\n    var code = null;\n    var match = null;\n    var i = null;\n    for (i=0; i&lt;matches.length; i++) {\n      if (matches[i].code == '%') { substitution = '%' }\n      else if (matches[i].code == 'b') {\n        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));\n        substitution = sprintfWrapper.convert(matches[i], true);\n      }\n      else if (matches[i].code == 'c') {\n        matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));\n        substitution = sprintfWrapper.convert(matches[i], true);\n      }\n      else if (matches[i].code == 'd') {\n        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));\n        substitution = sprintfWrapper.convert(matches[i]);\n      }\n      else if (matches[i].code == 'f') {\n        matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));\n        substitution = sprintfWrapper.convert(matches[i]);\n      }\n      else if (matches[i].code == 'o') {\n        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));\n        substitution = sprintfWrapper.convert(matches[i]);\n      }\n      else if (matches[i].code == 's') {\n        matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)\n        substitution = sprintfWrapper.convert(matches[i], true);\n      }\n      else if (matches[i].code == 'x') {\n        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));\n        substitution = sprintfWrapper.convert(matches[i]);\n      }\n      else if (matches[i].code == 'X') {\n        matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));\n        substitution = sprintfWrapper.convert(matches[i]).toUpperCase();\n      }\n      else {\n        substitution = matches[i].match;\n      }\n      newString += strings[i];\n      newString += substitution;\n    }\n    newString += strings[i];\n    return newString;\n  },\n  convert : function(match, nosign){\n    if (nosign) {\n      match.sign = '';\n    } else {\n      match.sign = match.negative ? '-' : match.sign;\n    }\n    var l = match.min - match.argument.length + 1 - match.sign.length;\n    var pad = new Array(l &lt; 0 ? 0 : l).join(match.pad);\n    if (!match.left) {\n      if (match.pad == \"0\" || nosign) {\n        return match.sign + pad + match.argument;\n      } else {\n        return pad + match.sign + match.argument;\n      }\n    } else {\n      if (match.pad == \"0\" || nosign) {\n        return match.sign + match.argument + pad.replace(\/0\/g, ' ');\n      } else {\n        return match.sign + match.argument + pad;\n      }\n    }\n  }\n}\nsprintf = sprintfWrapper.init;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttp:\/\/www.jb51.net\/article\/60398.htm\uff08\u6211\u77e5\u9053\u8fd9\u4e0d\u662f\u539f\u4f5c\u8005\uff0c\u8bf7\u77e5 &hellip; <a href=\"https:\/\/loveuhouse.cn\/wordpress\/?p=209\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">JS\u5b9e\u73b0php\u7684printf\u65b9\u6cd5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209"}],"collection":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=209"}],"version-history":[{"count":3,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":212,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/212"}],"wp:attachment":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}