{"id":27,"date":"2025-04-01T10:35:37","date_gmt":"2025-04-01T02:35:37","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/?p=27"},"modified":"2025-04-01T10:35:37","modified_gmt":"2025-04-01T02:35:37","slug":"%e5%9c%a8%e4%bd%bf%e7%94%a8php%e5%b0%86%e5%9b%be%e7%89%87%e6%9c%ac%e5%9c%b0%e5%8c%96%e7%9a%84%e8%bf%87%e7%a8%8b%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e5%9b%be%e7%89%87src%e5%9c%b0","status":"publish","type":"post","link":"https:\/\/www.zhaozhao123.cn\/php\/post\/27.html","title":{"rendered":"\u5728\u4f7f\u7528php\u5c06\u56fe\u7247\u672c\u5730\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u68c0\u6d4b\u56fe\u7247src\u5730\u5740\u662f\u5b8c\u6574\u7684\u7edd\u5bf9\u5730\u5740\u800c\u4e0d\u662f\u76f8\u5bf9\u5730\u5740\uff1f"},"content":{"rendered":"\n<p>\u5728PHP\u4e2d\u68c0\u6d4b\u56fe\u7247<code>src<\/code>\u5730\u5740\u662f\u5426\u4e3a\u5b8c\u6574\u7684\u7edd\u5bf9\u5730\u5740\uff08\u800c\u975e\u76f8\u5bf9\u5730\u5740\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1. \u4f7f\u7528PHP\u5185\u7f6e\u51fd\u6570<code>parse_url()<\/code>\u89e3\u6790URL\u7ed3\u6784<\/strong><br><code>parse_url()<\/code>\u53ef\u5c06URL\u5206\u89e3\u4e3a\u534f\u8bae\uff08scheme\uff09\u3001\u4e3b\u673a\u540d\uff08host\uff09\u3001\u8def\u5f84\uff08path\uff09\u7b49\u90e8\u5206\u3002\u901a\u8fc7\u5224\u65ad\u662f\u5426\u5b58\u5728<code>scheme<\/code>\u6216<code>host<\/code>\u5b57\u6bb5\uff0c\u53ef\u4ee5\u533a\u5206\u7edd\u5bf9\u5730\u5740\u4e0e\u76f8\u5bf9\u5730\u5740\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function isAbsoluteUrl($url) {\n    $parsed = parse_url($url);\n    return isset($parsed&#91;'scheme']) || isset($parsed&#91;'host']);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7edd\u5bf9\u5730\u5740\u793a\u4f8b<\/strong>\uff1a`<\/li>\n\n\n\n<li><strong>\u76f8\u5bf9\u5730\u5740\u793a\u4f8b<\/strong>\uff1a<code>\/images\/pic.jpg<\/code>\uff08\u4ec5\u5305\u542b<code>path<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>2. \u5904\u7406\u534f\u8bae\u76f8\u5bf9\u5730\u5740\uff08Protocol-relative URLs\uff09<\/strong><br>\u534f\u8bae\u76f8\u5bf9\u5730\u5740\u4ee5<code>\/\/<\/code>\u5f00\u5934\uff08\u5982<code>\/\/example.com\/image.jpg<\/code>\uff09\uff0c\u4f1a\u7ee7\u627f\u5f53\u524d\u9875\u9762\u7684\u534f\u8bae\uff08HTTP\/HTTPS\uff09\u3002\u9700\u5355\u72ec\u68c0\u6d4b\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function isAbsoluteUrl($url) {\n    $parsed = parse_url($url);\n    \/\/ \u68c0\u6d4b\u534f\u8bae\u6216\u534f\u8bae\u76f8\u5bf9\u5730\u5740\n    return isset($parsed&#91;'scheme']) || isset($parsed&#91;'host']) || (strpos($url, '\/\/') === 0);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u793a\u4f8b<\/strong>\uff1a<code>\/\/example.com\/image.jpg<\/code>\u4f1a\u88ab\u89c6\u4e3a\u7edd\u5bf9\u5730\u5740\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>3. \u6b63\u5219\u8868\u8fbe\u5f0f\u5feb\u901f\u5339\u914d<\/strong><br>\u5bf9\u4e8e\u7b80\u5355\u573a\u666f\uff0c\u53ef\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u76f4\u63a5\u5339\u914d\u7edd\u5bf9\u5730\u5740\u7684\u5e38\u89c1\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function isAbsoluteUrl($url) {\n    return preg_match('\/^(https?:)?\\\/\\\/\/i', $url) || preg_match('\/^\\\/\\\/\/', $url);\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5339\u914d\u4ee5`<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>4. \u7efc\u5408\u65b9\u6cd5\uff08\u63a8\u8350\uff09<\/strong><br>\u7ed3\u5408\u4e0a\u8ff0\u4e24\u79cd\u65b9\u5f0f\uff0c\u63d0\u9ad8\u68c0\u6d4b\u7684\u51c6\u786e\u6027\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function isAbsoluteUrl($url) {\n    \/\/ \u5feb\u901f\u68c0\u67e5\u534f\u8bae\u6216\u534f\u8bae\u76f8\u5bf9\u5730\u5740\n    if (preg_match('\/^(https?:)?\\\/\\\/\/i', $url) || strpos($url, '\/\/') === 0) {\n        return true;\n    }\n    \/\/ \u89e3\u6790URL\u7ed3\u6784\u8fdb\u4e00\u6b65\u9a8c\u8bc1\n    $parsed = parse_url($url);\n    return isset($parsed&#91;'scheme']) || isset($parsed&#91;'host']);\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u5173\u952e\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8fb9\u7f18\u60c5\u51b5\u5904\u7406<\/strong>\uff1a<\/li>\n\n\n\n<li><code>data:image\/png;base64...<\/code>\u7b49\u6570\u636eURI\u4f1a\u88ab\u8bc6\u522b\u4e3a\u7edd\u5bf9\u5730\u5740\uff08\u542b<code>scheme<\/code>\u5b57\u6bb5\uff09\uff0c\u9700\u6839\u636e\u4e1a\u52a1\u903b\u8f91\u989d\u5916\u8fc7\u6ee4\u3002<\/li>\n\n\n\n<li>\u8def\u5f84\u4ee5<code>\/<\/code>\u5f00\u5934\uff08\u5982<code>\/images\/pic.jpg<\/code>\uff09\u5c5e\u4e8e\u6839\u76f8\u5bf9\u8def\u5f84\uff0c\u4f46\u901a\u5e38\u89c6\u4e3a\u76f8\u5bf9\u5730\u5740\uff0c\u9700\u4e0e\u5b8c\u6574URL\u533a\u5206\u3002<\/li>\n\n\n\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u7528\u4e8e\u5feb\u901f\u5339\u914d\uff0c\u800c<code>parse_url()<\/code>\u66f4\u7cbe\u51c6\uff0c\u53ef\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong><em>\u4ee3\u7801\u4f7f\u7528\u793a\u4f8b<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$imageSrc = '\nif (isAbsoluteUrl($imageSrc)) {\n    echo \"\u7edd\u5bf9\u5730\u5740\uff0c\u65e0\u9700\u672c\u5730\u5316\u5904\u7406\";\n} else {\n    echo \"\u76f8\u5bf9\u5730\u5740\uff0c\u9700\u8f6c\u6362\u4e3a\u672c\u5730\u8def\u5f84\";\n}<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u9ad8\u6548\u68c0\u6d4b\u56fe\u7247\u5730\u5740\u7c7b\u578b\uff0c\u786e\u4fdd\u672c\u5730\u5316\u6d41\u7a0b\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728PHP\u4e2d\u68c0\u6d4b\u56fe\u7247src\u5730\u5740\u662f\u5426\u4e3a\u5b8c\u6574\u7684\u7edd\u5bf9\u5730\u5740\uff08\u800c\u975e\u76f8\u5bf9\u5730\u5740\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a 1. \u4f7f\u7528PHP\u5185\u7f6e\u51fd\u6570parse_url()\u89e3\u6790URL\u7ed3\u6784parse_url()\u53ef\u5c06URL\u5206\u89e3\u4e3a\u534f\u8bae\uff08scheme\uff09\u3001\u4e3b\u673a\u540d\uff08host\uff09\u3001\u8def\u5f84\uff08path\uff09\u7b49\u90e8\u5206\u3002\u901a\u8fc7\u5224\u65ad\u662f\u5426\u5b58\u5728scheme\u6216host\u5b57\u6bb5\uff0c\u53ef\u4ee5\u533a\u5206\u7edd\u5bf9\u5730\u5740\u4e0e\u76f8\u5bf9..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"tuisongtax":[],"class_list":["post-27","post","type-post","status-publish","format-standard","hentry","category-jswz"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/27","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/comments?post=27"}],"version-history":[{"count":0,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=27"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}