我有一些网址,如下面的网址:
https://example.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?的 Q = 1280wq
https://example.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?的 Q = 5080wq
https://example.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?的 Q = 860
https://example.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?的 Q = 86058
现在我想用{58}之类的常量值替换q
值,如何在PHP中使用str_replace
进行此操作?
答案 0 :(得分:1)
尝试这种方式:
$content = "https://mysite.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?q=1280wq";
$content = preg_replace("q=\w+", "", $content);
答案 1 :(得分:0)
$url = 'https://mysite.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?q=1280wq';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query);
echo $q;
答案 2 :(得分:0)
可以通过这样的str_replace来完成:
<?php
$myString = 'https://example.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs?q=1280wq';
$myNewString = str_replace('?q=', '/589/', $myString); //this line replaces the '?q=' with '/589/'
echo $myNewString; //OUTPUT: https://example.com/xyz/jjJAjsjahjsjhUAUiOAkskpAs/589/1280wq
?>