2
怎么把原来的 url 得到的某个地址,替换其中的域名呢?
source link: https://www.v2ex.com/t/901497
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
8 条回复 • 2022-12-10 14:47:58 +08:00
GreatAuk 7 小时 18 分钟前 |
muzihuaner 7 小时 16 分钟前 via Android 可以问一下 chatgpt😂
|
dongisking 7 小时 9 分钟前 ```
$str = 'xxx.com/sadasd.jpg'; $replace = 'aa.com'; $search = 'xxx.com'; echo str_ireplace($search, $replace, $str); ``` |
Thecosy 6 小时 44 分钟前 @muzihuaner 如果你想替换 URL 中的域名,你可以使用字符串操作来构造新的 URL 。下面是一个示例:
Copy code // Define the old and new domain names const oldDomain = "old-domain.com"; const newDomain = "new-domain.com"; // Define the URL const url = "http://old-domain.com/path/to/page.html"; // Replace the domain name in the URL const newUrl = url.replace(oldDomain, newDomain); // Print the new URL console.log(newUrl); // "http://new-domain.com/path/to/page.html" 在这个例子中,我们定义了原来的域名和新的域名,然后使用 String.replace() 方法来替换 URL 中的域名。 希望这对你有帮助! 来自 chatgpt |
neotheone2333 6 小时 36 分钟前 可以多问一句:我想要一个更通用的方法
如果你想要一个更通用的方法来替换 URL 中的域名,那么你可以使用一个函数来实现。例如,下面是一个简单的函数,它可以接收一个 URL 和一个新的域名,然后返回替换后的 URL 。 Copy code function replaceDomain(url, newDomain) { let oldDomain = /[^/]*?(?=\/)/; return url.replace(oldDomain, newDomain); } 在这个函数中,我们首先使用正则表达式 /[^/]*?(?=\/)/ 来匹配 URL 中的域名。这个正则表达式的工作原理是,先匹配所有不含斜杠的字符,然后在后面加上一个非贪婪的匹配,用来匹配第一个出现的斜杠。 然后,我们使用 replace 方法来替换原来的域名。最后,我们返回替换后的 URL 。 下面是一个使用这个函数的例子: Copy code let oldUrl = 'xxx.com/sadasd.jpg'; let newUrl = replaceDomain(oldUrl, 'aaa.com'); console.log(newUrl); // 输出 aaa.com/sadasd.jpg 希望这些信息能够帮助你。如果你还有其他问题,请继续提问。 |
ChoateYao 5 小时 5 分钟前 用 parse_url 想想怎么替换就怎么替换。
|
SkyRing 4 小时 25 分钟前 有点看不懂,我还是抽空在补习一下好了。不过解决了,虽然是 js 随便糊了一下。先用着好了。
|
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK