使用 php,我的目標是將 url 上存在的任何引數附加到頁面上的所有錨點(鏈接)中。
- 如果鏈接已包含引數:將 url 查詢添加到引數中。
訪問的頁面網址:
site.com/?location=brazil&sex=female
頁面“site.com”代碼:
<!DOCTYPE html>
<html>
<body>
<a href="https://example.com/"> HELLO1 </a>
<! ––I need it to become https://example.com/?location=brazil&sex=female ––>
<a href="https://example.com/?human=yes"> HELLO2 </a>
<! ––I need it to become https://example.com/?human=yes&location=brazil&sex=female ––>
<?php
?>
</body>
</html>
我已經在 javascript 中完成了它,但遇到了一些問題。有人可以幫我一個php代碼嗎?問候。
uj5u.com熱心網友回復:
I hope this will help you out
<?php
//to get current url
$url = "https://";
//Append the host(domain name, ip) to the URL.
$url.= $_SERVER['HTTP_HOST'];
//Append the requested resource location to the URL
$url.= $_SERVER['REQUEST_URI'];
//for example the url i considered
$url_example = 'https://example.com?q=string&f=true&id=1233&sort=true';
//explode function will breakdown string in array
$host = explode('https://example.com?', $url_example); //here replace $url_example with $url as i used for demo purpose only
// accessing the second part of array
echo $host[1]; //for checking
?>
<!DOCTYPE html>
<html>
<body>
<a href="https://example.com?<?= $host[1] ?>">Hello!</a>
<a href="https://example.com?human=yes&<?= $host[1] ?>">Hello2</a>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/441384.html
標籤:php
上一篇:觀察一個RxSwift.BehaviorRelay就在**before**發生變化
下一篇:登錄系統中的關聯陣列
