{"id":152,"date":"2010-08-31T16:55:05","date_gmt":"2010-08-31T08:55:05","guid":{"rendered":"http:\/\/zduo.me\/blog\/?p=152"},"modified":"2010-08-31T17:02:16","modified_gmt":"2010-08-31T09:02:16","slug":"php%e3%80%81js%e4%b8%ad%e7%9a%84replace%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/zduo.me\/blog\/?p=152","title":{"rendered":"php\u3001js\u4e2d\u7684replace\u51fd\u6570"},"content":{"rendered":"<p>PHP<\/p>\n<p>str_replace(find,replace,string,count)<\/p>\n<p>find \u5fc5\u9700\u3002\u89c4\u5b9a\u8981\u67e5\u627e\u7684\u503c\u3002<br \/>\nreplace \u5fc5\u9700\u3002\u89c4\u5b9a\u66ff\u6362 find \u4e2d\u7684\u503c\u7684\u503c\u3002<br \/>\nstring \u5fc5\u9700\u3002\u89c4\u5b9a\u88ab\u641c\u7d22\u7684\u5b57\u7b26\u4e32\u3002<br \/>\ncount \u53ef\u9009\u3002\u4e00\u4e2a\u53d8\u91cf\uff0c\u5bf9\u66ff\u6362\u6570\u8fdb\u884c\u8ba1\u6570\u3002<\/p>\n<p>\u4f8b\uff1a<code>str_replace(\"world\",\"John\",\"Hello world!\")<\/code>; \u8fd4\u56deHello John!<\/p>\n<p>\u4eca\u5929\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u91cc\u7684\u6362\u884c\u7b26 \\n\u00a0\uff0c<code>str_replace(\"\\n\",\"&lt;br&gt;\",$str)<\/code>;\u8c8c\u4f3c\u4e0d\u8d77\u4f5c\u7528\uff0c\u4e4b\u524d\u7528\u8fc7\u662f\u53ef\u4ee5\u7684\uff0c\u90c1\u95f7\u3002\u4e0d\u8fc7\u540e\u6765\u53d1\u73b0php\u91cc\u9762\u81ea\u5e26\u4e86\u4e00\u4e2a\u66ff\u6362\u6362\u884c\u7b26\u7684\u51fd\u6570nl2br()\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u65b0\u884c (\\n) \u4e4b\u524d\u63d2\u5165 HTML \u6362\u884c\u7b26 (&lt;br \/&gt;)\u3002\u7528\u6cd5nl2br(string)<\/p>\n<p>JS<\/p>\n<p>stringObject.replace(regexp\/substr,replacement)<br \/>\nregexp\/substr \u5fc5\u9700\u3002\u89c4\u5b9a\u5b50\u5b57\u7b26\u4e32\u6216\u8981\u66ff\u6362\u7684\u6a21\u5f0f\u7684 RegExp \u5bf9\u8c61\u3002\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u8be5\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5219\u5c06\u5b83\u4f5c\u4e3a\u8981\u68c0\u7d22\u7684\u76f4\u63a5\u91cf\u6587\u672c\u6a21\u5f0f\uff0c\u800c\u4e0d\u662f\u9996\u5148\u88ab\u8f6c\u6362\u4e3a RegExp \u5bf9\u8c61\u3002<br \/>\nreplacement \u5fc5\u9700\u3002\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u89c4\u5b9a\u4e86\u66ff\u6362\u6587\u672c\u6216\u751f\u6210\u66ff\u6362\u6587\u672c\u7684\u51fd\u6570\u3002<\/p>\n<p>js\u4e2d\u76f4\u63a5\u4f7f\u7528str.replace(&#8220;a&#8221;,&#8221;b&#8221;)\u65f6\uff0c\u53ea\u4f1a\u628astr\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684a\u66ff\u6362\u4e3ab\uff0c\u5982\u679c\u8981\u66ff\u6362\u6240\u6709\u7684a\u9700\u8981\u7528\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\uff1astr.replace(\/a\/gi,&#8221;b&#8221;)\u8fd9\u6837\u5199\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP str_replace(find,replace,string,count) find \u5fc5\u9700\u3002\u89c4\u5b9a\u8981\u67e5\u627e\u7684\u503c\u3002 replace \u5fc5\u9700\u3002\u89c4\u5b9a\u66ff\u6362 find \u4e2d\u7684\u503c\u7684\u503c\u3002 string \u5fc5\u9700\u3002\u89c4\u5b9a\u88ab\u641c\u7d22\u7684\u5b57\u7b26\u4e32\u3002 count \u53ef\u9009\u3002\u4e00\u4e2a\u53d8\u91cf\uff0c\u5bf9\u66ff\u6362\u6570\u8fdb\u884c\u8ba1\u6570\u3002 \u4f8b\uff1astr_replace(&#8220;world&#8221;,&#8221;John&#8221;,&#8221;Hello world!&#8221;); \u8fd4\u56deHello John! \u4eca\u5929\u7528\u8fd9\u4e2a\u51fd\u6570\u6765\u66ff\u6362\u5b57\u7b26\u4e32\u91cc\u7684\u6362\u884c\u7b26 \\n\u00a0\uff0cstr_replace(&#8220;\\n&#8221;,&#8221;&lt;br&gt;&#8221;,$str);\u8c8c\u4f3c\u4e0d\u8d77\u4f5c\u7528\uff0c\u4e4b\u524d\u7528\u8fc7\u662f\u53ef\u4ee5\u7684\uff0c\u90c1\u95f7\u3002\u4e0d\u8fc7\u540e\u6765\u53d1\u73b0php\u91cc\u9762\u81ea\u5e26\u4e86\u4e00\u4e2a\u66ff\u6362\u6362\u884c\u7b26\u7684\u51fd\u6570nl2br()\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5728\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u65b0\u884c (\\n) \u4e4b\u524d\u63d2\u5165 HTML \u6362\u884c\u7b26 (&lt;br \/&gt;)\u3002\u7528\u6cd5nl2br(string) JS stringObject.replace(regexp\/substr,replacement) regexp\/substr \u5fc5\u9700\u3002\u89c4\u5b9a\u5b50\u5b57\u7b26\u4e32\u6216\u8981\u66ff\u6362\u7684\u6a21\u5f0f\u7684 RegExp \u5bf9\u8c61\u3002\u8bf7\u6ce8\u610f\uff0c\u5982\u679c\u8be5\u503c\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5219\u5c06\u5b83\u4f5c\u4e3a\u8981\u68c0\u7d22\u7684\u76f4\u63a5\u91cf\u6587\u672c\u6a21\u5f0f\uff0c\u800c\u4e0d\u662f\u9996\u5148\u88ab\u8f6c\u6362\u4e3a RegExp \u5bf9\u8c61\u3002 replacement \u5fc5\u9700\u3002\u4e00\u4e2a\u5b57\u7b26\u4e32\u503c\u3002\u89c4\u5b9a\u4e86\u66ff\u6362\u6587\u672c\u6216\u751f\u6210\u66ff\u6362\u6587\u672c\u7684\u51fd\u6570\u3002 js\u4e2d\u76f4\u63a5\u4f7f\u7528str.replace(&#8220;a&#8221;,&#8221;b&#8221;)\u65f6\uff0c\u53ea\u4f1a\u628astr\u4e2d\u7b2c\u4e00\u4e2a\u51fa\u73b0\u7684a\u66ff\u6362\u4e3ab\uff0c\u5982\u679c\u8981\u66ff\u6362\u6240\u6709\u7684a\u9700\u8981\u7528\u5230\u6b63\u5219\u8868\u8fbe\u5f0f\uff1astr.replace(\/a\/gi,&#8221;b&#8221;)\u8fd9\u6837\u5199\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14,8],"tags":[50,51],"_links":{"self":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/152"}],"collection":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152"}],"version-history":[{"count":4,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":156,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions\/156"}],"wp:attachment":[{"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zduo.me\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}