{"id":534,"date":"2025-06-11T00:28:09","date_gmt":"2025-06-10T16:28:09","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/szyzfc\/534.html"},"modified":"2025-06-11T00:28:09","modified_gmt":"2025-06-10T16:28:09","slug":"php-%e4%bb%8e%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e6%8f%90%e5%8f%96url","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/534.html","title":{"rendered":"PHP: \u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6URL"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"&#23567;&#36213;&#30721;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<p class=\"my-0\">&#23567;&#36213;&#30721;&#29422;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22909;&#30340;&#65292;&#35753;&#25105;&#20204;&#26469;&#35752;&#35770;&#22914;&#20309;&#20351;&#29992;PHP&#20174;&#23383;&#31526;&#20018;&#20013;&#25552;&#21462;URL&#12290;&#20551;&#35774;&#25105;&#20204;&#26377;&#19968;&#20010;&#21253;&#21547;&#22810;&#20010;URL&#30340;&#25991;&#26412;&#65292;&#24182;&#19988;&#24076;&#26395;&#20174;&#20013;&#25552;&#21462;&#20986;&#26469;&#12290;<\/p>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#39318;&#20808;&#65292;&#25105;&#20204;&#38656;&#35201;&#23450;&#20041;&#19968;&#20010;&#20989;&#25968;&#26469;&#35299;&#26512;&#23383;&#31526;&#20018;&#24182;&#25552;&#21462;URL&#12290;&#25105;&#20204;&#21487;&#20197;&#20351;&#29992;&#27491;&#21017;&#34920;&#36798;&#24335;&#26469;&#21305;&#37197;URL&#12290;<\/p>\n<pre><code class=\"language-php\">&lt;?php\nfunction extractUrls($text) {\n    \/\/ &#23450;&#20041;&#27491;&#21017;&#34920;&#36798;&#24335;&#27169;&#24335;\n    $pattern = '\/(http|https):\/\/[^s]+\/';\n\n    \/\/ &#20351;&#29992;preg_match_all&#26597;&#25214;&#25152;&#26377;&#21305;&#37197;&#39033;\n    preg_match_all($pattern, $text, $matches);\n\n    \/\/ &#36820;&#22238;&#25214;&#21040;&#30340;&#25152;&#26377;URL\n    return $matches[0];\n}\n\n\/\/ &#31034;&#20363;&#25991;&#26412;\n$text = \"Check out this website: https:\/\/www.example.com and this one too: http:\/\/example.org\";\n$urls = extractUrls($text);\n\n\/\/ &#36755;&#20986;&#32467;&#26524;\necho \"Extracted URLs:n\";\nprint_r($urls);\n?&gt;<\/code><\/pre>\n<h3>&#35299;&#37322;<\/h3>\n<ol>\n<li>\n<p><strong>&#23450;&#20041;&#27491;&#21017;&#34920;&#36798;&#24335;<\/strong>&#65306;<\/p>\n<ul>\n<li><code>http(s)<\/code>: &#21305;&#37197;HTTP&#25110;HTTPS&#21327;&#35758;&#12290;<\/li>\n<li><code>:\/\/<\/code>: &#21305;&#37197;&#21452;&#26012;&#26464; <code>:\/\/<\/code>&#12290;<\/li>\n<li><code>[^s]+<\/code>: &#21305;&#37197;&#19968;&#20010;&#25110;&#22810;&#20010;&#38750;&#31354;&#30333;&#23383;&#31526;&#65292;&#21363;URL&#30340;&#19968;&#37096;&#20998;&#12290;<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>&#20351;&#29992;<code>preg_match_all<\/code><\/strong>&#65306;<\/p>\n<ul>\n<li><code>$pattern<\/code> &#26159;&#27491;&#21017;&#34920;&#36798;&#24335;&#30340;&#27169;&#24335;&#12290;<\/li>\n<li><code>$text<\/code> &#26159;&#35201;&#25628;&#32034;&#30340;&#25991;&#26412;&#12290;<\/li>\n<li><code>$matches<\/code> &#25968;&#32452;&#23558;&#23384;&#20648;&#25152;&#26377;&#21305;&#37197;&#21040;&#30340;URL&#12290;<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>&#36820;&#22238;&#32467;&#26524;<\/strong>&#65306;<\/p>\n<ul>\n<li><code>return $matches[0];<\/code> &#36820;&#22238;&#25214;&#21040;&#30340;&#25152;&#26377;URL&#25968;&#32452;&#12290;<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>&#36755;&#20986;&#32467;&#26524;<\/h3>\n<p>&#36816;&#34892;&#19978;&#36848;&#20195;&#30721;&#21518;&#65292;&#36755;&#20986;&#23558;&#20250;&#26159;&#65306;<\/p>\n<pre><code>Extracted URLs:\nArray\n(\n    [0] =&gt; https:\/\/www.example.com\n    [1] =&gt; http:\/\/example.org\n)<\/code><\/pre>\n<p>&#36825;&#26679;&#23601;&#25104;&#21151;&#22320;&#20174;&#23383;&#31526;&#20018;&#20013;&#25552;&#21462;&#20102;&#25152;&#26377;&#30340;URL&#12290;&#20320;&#21487;&#20197;&#26681;&#25454;&#38656;&#35201;&#20462;&#25913;&#27491;&#21017;&#34920;&#36798;&#24335;&#20197;&#36866;&#24212;&#19981;&#21516;&#30340;URL&#26684;&#24335;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#23567;&#39532;&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<p class=\"my-0\">&#23567;&#39532;&#35762;&#24072;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>&#22312;&#23548;&#33322;&#32593;&#39029;&#31243;&#24207;&#21270;&#25110;&#35299;&#26512;PHP&#20013;&#30340;&#25991;&#26412;&#25968;&#25454;&#26102;&#65292;&#24744;&#21487;&#33021;&#32463;&#24120;&#38656;&#35201;&#20174;&#23383;&#31526;&#20018;&#20013;&#25552;&#21462;URL&#12290;&#36825;&#31181;&#25216;&#33021;&#23545;&#20110;&#32593;&#39029;&#29228;&#34411;&#12289;&#25968;&#25454;&#36801;&#31227;&#21644;SEO&#24037;&#20855;&#24320;&#21457;&#23588;&#20854;&#26377;&#29992;&#12290;&#22312;&#36825;&#31687;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#23558;&#25506;&#32034;&#22810;&#31181;&#26041;&#27861;&#20197;&#29992;PHP&#23454;&#29616;&#36825;&#19968;&#30446;&#26631;&#65292;&#38543;&#30528;&#25105;&#20204;&#36880;&#27493;&#28145;&#20837;&#65292;&#25105;&#20204;&#30340;&#24037;&#20855;&#31665;&#20063;&#23558;&#20174;&#22522;&#30784;&#21040;&#39640;&#32423;&#19981;&#26029;&#21457;&#23637;&#12290;<\/p><h2>&#22522;&#26412;URL&#25552;&#21462;<\/h2><p>&#39318;&#20808;&#65292;&#25105;&#20204;&#23558;&#35752;&#35770;&#20351;&#29992;PHP&#20869;&#32622;&#20989;&#25968;&#25552;&#21462;URL&#30340;&#26368;&#31616;&#21333;&#26041;&#27861;&#12290;egex_match_all()&#20989;&#25968;&#26159;&#19968;&#20010;&#24378;&#22823;&#30340;&#24037;&#20855;&#65292;&#21487;&#20197;&#25628;&#32034;&#30001;&#27491;&#21017;&#34920;&#36798;&#24335;&#23450;&#20041;&#30340;&#27169;&#24335;&#22312;&#23383;&#31526;&#20018;&#20013;&#30340;&#23384;&#22312;&#24773;&#20917;&#12290;&#19968;&#20010;&#22522;&#26412;&#30340;URL&#25552;&#21462;&#27491;&#21017;&#34920;&#36798;&#24335;&#21487;&#33021;&#20250;&#30475;&#36215;&#26469;&#20687;&#36825;&#26679;&#65306;<\/p><pre><code>\/\/ The input string containing URLs\n$string = 'Check out https:\/\/www.example.com and http:\/\/www.foo.com.';\n\n\/\/ Regular Expression Pattern for a basic URL\n$pattern = '\/b(?:https?:\/\/)[a-zA-Z0-9.-]+(?:.[a-zA-Z]{2,})(?:\/S*)?\/';\n\n\/\/ Array to hold the matched URLs\n$matches = [];\n\n\/\/ Perform the pattern match\npreg_match_all($pattern, $string, $matches);\n\n\/\/ Print the matches\nprint_r($matches[0]);\n<\/code><\/pre><h2>&#25913;&#36827;URL&#25552;&#21462;&#30340;&#27491;&#21017;&#34920;&#36798;&#24335;&#26041;&#27861;<\/h2><p>&#38543;&#30528;&#25105;&#20204;&#28145;&#20837;&#30740;&#31350;&#65292;&#25105;&#20204;&#21487;&#20197;&#23545;&#27491;&#21017;&#34920;&#36798;&#24335;&#36827;&#34892;&#26356;&#32454;&#33268;&#30340;&#35843;&#25972;&#65292;&#20197;&#26356;&#22909;&#22320;&#22788;&#29702;&#36793;&#32536;&#24773;&#20917;&#21644;&#19981;&#21516;&#30340;URL&#26684;&#24335;&#65306;<\/p><pre><code>\/\/ Improved Regular Expression Pattern\n$pattern = '\/b(?:https?:\/\/)?(?:www.)?[a-zA-Z0-9.-]+.w+(?:\/[w\/.?-]*)?\/';\n\/\/ Rest of the code is the same...\n<\/code><\/pre><p>&#35813;&#27491;&#21017;&#34920;&#36798;&#24335;&#32771;&#34385;&#20102;&#21487;&#36873;&#30340;&#21327;&#35758;&#21644;&#23376;&#22495;&#21517;&#65292;&#20197;&#21450;&#21508;&#31181;URL&#36335;&#24452;&#32452;&#20214;&#12290;<\/p><h2>&#20351;&#29992;PHP&#36807;&#28388;&#22120;<\/h2><p>&#38500;&#20102;&#27491;&#21017;&#34920;&#36798;&#24335;&#65292;PHP &#36824;&#25552;&#20379;&#20102;&#36807;&#28388;&#22120;&#26469;&#39564;&#35777;&#21644;&#28165;&#29702;&#25968;&#25454;&#65292;&#21253;&#25324; URL&#12290;&#36825;&#37324;&#25105;&#20204;&#28436;&#31034;&#22914;&#20309;&#20351;&#29992;&#36825;&#20123;&#36807;&#28388;&#22120;&#12290;<code>filter_var<\/code>&#23545;&#19981;&#36215;&#65292;&#24744;&#30340;&#20449;&#24687;&#19981;&#23436;&#25972;&#65292;&#25105;&#26080;&#27861;&#29702;&#35299;&#24744;&#35201;&#34920;&#36798;&#30340;&#24847;&#24605;&#12290;&#22914;&#26524;&#24744;&#33021;&#25552;&#20379;&#26356;&#22810;&#32972;&#26223;&#20449;&#24687;&#25110;&#37325;&#26032;&#25551;&#36848;&#24744;&#30340;&#38656;&#27714;&#65292;&#25105;&#20250;&#24456;&#20048;&#24847;&#20026;&#24744;&#25552;&#20379;&#24110;&#21161;&#12290;<code>FILTER_VALIDATE_URL<\/code>&#25351;&#20196;&#65306;&#26597;&#25214;&#24182;&#39564;&#35777;URL&#65306;<\/p><pre><code>\/\/ Split the input string by spaces or any other delimiters you expect\n$parts = preg_split('\/s+\/', $string);\n\n\/\/ Array to hold valid URLs\n$validURLs = [];\n\nforeach ($parts as $part) {\n    if (filter_var($part, FILTER_VALIDATE_URL) !== false) {\n        $validURLs[] = $part;\n    }\n}\n\n\/\/ Print the valid URLs\nprint_r($validURLs);\n<\/code><\/pre><h2>&#39640;&#32423;URL&#25552;&#21462;<\/h2><p>&#22312;&#26356;&#22797;&#26434;&#30340;&#22330;&#26223;&#19979;&#65292;&#22914;&#22788;&#29702;&#32534;&#30721;&#30340;URL&#12289;&#23884;&#20837;&#22312;&#33050;&#26412;&#25110;&#26679;&#24335;&#20013;&#30340;URL&#31561;&#65292;&#38656;&#35201;&#39069;&#22806;&#30340;&#35299;&#26512;&#36923;&#36753;&#12290;&#33021;&#22815;&#26356;&#28145;&#20837;&#29702;&#35299;HTML&#32467;&#26500;&#30340;&#24211;&#25110;&#20989;&#25968;&#21487;&#20197;&#24110;&#21161;&#65306;<\/p><pre><code>\/\/ For example, using the PHP Simple HTML DOM Parser:\n\n\/\/ Assume we're using the simple_html_dom library available through Composer. Be sure you have included the library in your project.\n\n\/\/ Create a DOM object from a string\n$html = str_get_html($string);\n\n\/\/ Find all the links\nforeach($html-&gt;find('a') as $element) {\n    echo $element-&gt;href . 'n';\n}\n\n\/\/ Remember to handle script, style, or encoded URLs differently\n\/\/ Additional parsing logic here\n<\/code><\/pre><p>&#36825;&#38656;&#35201;&#20320;&#22788;&#29702;&#26356;&#22810;&#30340;&#26696;&#20214;&#65292;&#20063;&#21487;&#33021;&#20250;&#20351;&#29992;&#19968;&#20123;&#38468;&#21152;&#30340;&#24211;&#26469;&#36827;&#34892;&#21487;&#38752;&#30340;HTML&#35299;&#26512;&#12290;<\/p><h2>&#32467;&#35770;&#12290;<\/h2><p>&#22312;&#36825;&#27425;&#25945;&#31243;&#20013;&#65292;&#25105;&#20204;&#20171;&#32461;&#20102;&#22914;&#20309;&#20174;&#23383;&#31526;&#20018;&#20013;&#25552;&#21462;URL&#30340;&#26041;&#27861;&#65292;&#22312;PHP&#20013;&#20174;&#31616;&#21333;&#30340;&#27491;&#21017;&#34920;&#36798;&#24335;&#24320;&#22987;&#65292;&#36880;&#27493;&#36807;&#28193;&#21040;&#21033;&#29992;PHP&#20869;&#32622;&#20989;&#25968;&#21644;&#22806;&#37096;&#24211;&#30340;&#39640;&#32423;&#26041;&#27861;&#12290;&#29616;&#22312;&#65292;&#20320;&#24212;&#35813;&#23545;&#22788;&#29702;&#36825;&#19968;&#24120;&#35265;&#20219;&#21153;&#26377;&#24456;&#22909;&#30340;&#29702;&#35299;&#65292;&#24182;&#33021;&#22815;&#26681;&#25454;&#26356;&#22797;&#26434;&#30340;&#22330;&#26223;&#25110;&#39033;&#30446;&#29305;&#23450;&#38656;&#27714;&#35843;&#25972;&#31034;&#20363;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&#23567;&#36213;&#30721;&#29422; &#22909;&#30340;&#65292;&#35753;&#25105;&#20204;&#26469;&#35752;&#35770;&#22914;&#20309;&#20351;&#29992;PHP&#20174;&#23383;&#038;#31..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[40],"tuisongtax":[],"class_list":["post-534","my1js","type-my1js","status-publish","hentry","my1js2nav-szyzfc"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=534"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=534"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=534"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}