{"id":183868,"date":"2024-05-08T21:55:02","date_gmt":"2024-05-08T13:55:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/183868.html"},"modified":"2024-05-08T21:55:09","modified_gmt":"2024-05-08T13:55:09","slug":"php%e5%b0%86width500px%e6%9b%bf%e6%8d%a2%e6%88%90width500%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/183868.html","title":{"rendered":"PHP\u5c06width:500px;\u66ff\u6362\u6210width=\\500\\\u600e\u4e48\u5199"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27074758\/358b3515-2dff-4bde-8199-659a50934a28.webp\" alt=\"PHP\u5c06width:500px;\u66ff\u6362\u6210width=\\500\\\u600e\u4e48\u5199\" \/><\/p>\n<p><p>\u5728PHP\u4e2d\uff0c\u5c06CSS\u5c5e\u6027<code>width:500px;<\/code>\u66ff\u6362\u6210<code>width=\\500<\/code>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u51fd\u6570<code>str_replace()<\/code>\u5b9e\u73b0\uff0c<strong>\u6838\u5fc3\u89c2\u70b9<\/strong>\u662f\u4f7f\u7528<strong>\u5b57\u7b26\u4e32\u66ff\u6362\u51fd\u6570<\/strong>\u3001<strong>\u7279\u522b\u6ce8\u610f\u8f6c\u4e49\u5b57\u7b26\u7684\u5e94\u7528<\/strong>\u3002\u4f7f\u7528<code>str_replace()<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u7279\u522b\u7559\u610f\u76ee\u6807\u5b57\u7b26\u4e32\u4e2d\u7279\u6b8a\u5b57\u7b26\u7684\u5904\u7406\u3002\u4f8b\u5982\uff0c<code>\\<\/code>\u662fPHP\u4e2d\u7684\u8f6c\u4e49\u5b57\u7b26\uff0c\u56e0\u6b64\u5728\u66ff\u6362\u89c4\u5219\u91cc\u9762\u9700\u8981\u4f7f\u7528<code>\\\\<\/code>\u6765\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3<code>str_replace()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>str_replace()<\/code>\u51fd\u6570\u5728PHP\u4e2d\u7528\u4e8e\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e9b\u90e8\u5206\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>str_replace($search, $replace, $subject)<\/code>\uff0c\u5176\u4e2d<code>$search<\/code>\u662f\u9700\u8981\u88ab\u66ff\u6362\u7684\u5b50\u5b57\u7b26\u4e32\uff0c<code>$replace<\/code>\u662f\u66ff\u6362\u540e\u7684\u5b57\u7b26\u4e32\uff0c<code>$subject<\/code>\u662f\u88ab\u641c\u7d22\u548c\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c<code>$search<\/code>\u548c<code>$replace<\/code>\u662f\u6570\u7ec4\uff0c\u5219<code>str_replace()<\/code>\u5c06\u8fdb\u884c\u591a\u6b21\u641c\u7d22\u548c\u66ff\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7279\u522b\u6ce8\u610f\u8f6c\u4e49\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5b57\u7b26\u4e32\u66ff\u6362\u65f6\uff0c\u9700\u8981\u7279\u522b\u6ce8\u610f\u8f6c\u4e49\u5b57\u7b26<code><\/code>\u7684\u4f7f\u7528\u3002\u5728PHP\u5b57\u7b26\u4e32\u4e2d\uff0c<code>\\<\/code>\u7528\u4f5c\u8f6c\u4e49\u5b57\u7b26\uff0c\u4f8b\u5982<code>\\n<\/code>\u8868\u793a\u6362\u884c\uff0c<code>\\\\<\/code>\u8868\u793a\u5b57\u9762\u91cf\u4e2d\u7684<code>\\<\/code>\u5b57\u7b26\u3002\u56e0\u6b64\uff0c\u5728\u66ff\u6362\u89c4\u5219\u4e2d\uff0c\u5982\u679c\u76ee\u7684\u662f\u5c06\u5b57\u7b26<code>\\<\/code>\u63d2\u5165\u5230\u5b57\u7b26\u4e32\u4e2d\uff0c\u9700\u8981\u4f7f\u7528<code>\\\\<\/code>\u6765\u8868\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u66ff\u6362\u89c4\u5219\u7f16\u5199<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>str_replace()<\/code>\u51fd\u6570\u66ff\u6362<code>width:500px;<\/code>\u4e3a<code>width=\\500<\/code>\u7684\u89c4\u5219\uff0c\u9700\u8981\u6ce8\u610f\u5c06\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7684<code>\\<\/code>\u7528<code>\\\\<\/code>\u8868\u793a\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$originalCss = &#039;width:500px;&#039;;<\/p>\n<p>$targetCss = str_replace(&#039;width:500px;&#039;, &#039;width=\\\\500\\\\&#039;, $originalCss);<\/p>\n<p>echo $targetCss;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fa<code>width=\\500<\/code>\uff0c\u5b9e\u73b0\u4e86\u6240\u9700\u7684\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fdb\u9636\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u6362\u903b\u8f91\uff0c\u5c24\u5176\u662f\u5f53\u66ff\u6362\u89c4\u5219\u4e0d\u662f\u9759\u6001\u7684\u6216\u8005\u9700\u8981\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u5177\u6709\u66f4\u590d\u6742\u7684\u6a21\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u914d\u5408<code>preg_replace()<\/code>\u51fd\u6570\u3002<code>preg_replace()<\/code>\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u80fd\u529b\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u590d\u6742\u7684\u6a21\u5f0f\u5339\u914d\u548c\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528<code>preg_replace()<\/code>\u8fdb\u884c\u66ff\u6362<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e0a\u8ff0\u8981\u6c42\uff0c\u82e5\u8003\u8651\u5230\u52a8\u6001\u5bbd\u5ea6\u503c\uff0c<code>preg_replace()<\/code>\u7684\u4f7f\u7528\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">&lt;?php<\/p>\n<p>$originalCss = &#039;width:500px;&#039;;<\/p>\n<p>$pattern = &#039;\/width:(\\d+)px;\/&#039;;<\/p>\n<p>$replacement = &#039;width=\\\\\\\\$1\\\\\\\\&#039;;<\/p>\n<p>$targetCss = preg_replace($pattern, $replacement, $originalCss);<\/p>\n<p>echo $targetCss;<\/p>\n<p>?&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>$pattern<\/code>\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7528\u4e8e\u5339\u914d\u8bf8\u5982<code>width:500px;<\/code>\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d<code>(\\d+)<\/code>\u6355\u83b7\u6570\u5b57\u90e8\u5206\u4ee5\u5907\u540e\u7528\u3002<code>$replacement<\/code>\u4e2d\u7684<code>$1<\/code>\u5f15\u7528\u4e86\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u6355\u83b7\u7ec4\uff08\u5373<code>\\d+<\/code>\u5339\u914d\u7684\u90e8\u5206\uff09\uff0c\u800c<code>\\\\\\<\/code>\u8868\u793a\u66ff\u6362\u7ed3\u679c\u4e2d\u7684\u5355\u4e2a<code>\\<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8ff0\u65b9\u6cd5\uff0cPHP\u80fd\u591f\u7075\u6d3b\u5730\u5c06CSS\u5c5e\u6027<code>width:500px;<\/code>\u8f6c\u6362\u4e3a<code>width=\\500<\/code>\uff0c\u65e0\u8bba\u662f\u91c7\u7528\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u8fd8\u662f\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5904\u7406\u66f4\u590d\u6742\u7684\u6a21\u5f0f\u66ff\u6362\u3002\u8fd9\u4f53\u73b0\u4e86PHP\u5728\u5b57\u7b26\u4e32\u5904\u7406\u65b9\u9762\u7684\u5f3a\u5927\u80fd\u529b\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728PHP\u4e2d\u5c06width:500px\u66ff\u6362\u4e3awidth=\\500\\\uff1f<\/strong><\/p>\n<p>\u5728PHP\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u51fd\u6570str_replace()\u5c06width:500px\u66ff\u6362\u4e3awidth=\\500\\\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-php\">$css = &quot;width:500px;&quot;;\n$modified_css = str_replace(&quot;width:500px;&quot;, &quot;width=\\\\500\\\\&quot;, $css);\necho $modified_css;\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u4f1a\u8f93\u51fawidth=\\500\\\u3002<\/p>\n<p><strong>2. \u5728PHP\u4e2d\u600e\u6837\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362width:500px\u4e3awidth=\\500\\\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u66ff\u6362\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528preg_replace()\u51fd\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-php\">$css = &quot;width:500px;&quot;;\n$modified_css = preg_replace(&quot;\/width:500px;\/&quot;, &quot;width=\\\\500\\\\&quot;, $css);\necho $modified_css;\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u8f93\u51fawidth=\\500\\\u3002<\/p>\n<p><strong>3. \u5728PHP\u4e2d\uff0c\u5982\u4f55\u5904\u7406,width:500px\u4e2d\u7684width\u503c\u5e76\u5c06\u5176\u66ff\u6362\u4e3awidth=\\500\\\uff1f<\/strong><\/p>\n<p>\u4e3a\u4e86\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5e76\u66ff\u6362\u5bbd\u5ea6\u503c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548cpreg_replace_callback()\u51fd\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-php\">$css = &quot;width:500px;&quot;;\n$modified_css = preg_replace_callback(&quot;\/width:(\\d+)px;\/&quot;, function($matches) {\n    $width = $matches[1];\n    return &quot;width=\\\\&quot; . $width . &quot;\\\\&quot;;\n}, $css);\necho $modified_css;\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u4f1a\u8f93\u51fawidth=\\500\\\uff0c\u5b83\u4f1a\u63d0\u53d6\u5bbd\u5ea6\u503c500\u5e76\u5c06\u5176\u66ff\u6362\u4e3awidth=\\500\\\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728PHP\u4e2d\uff0c\u5c06CSS\u5c5e\u6027width:500px;\u66ff\u6362\u6210width=\\500\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u66ff\u6362\u51fd\u6570str_r [&hellip;]","protected":false},"author":3,"featured_media":183872,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/183868"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=183868"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/183868\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/183872"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=183868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=183868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=183868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}