简体   繁体   中英

Remove characters from url

I have following url . Using php I would like to remove /?qa=ask&cat=2/ from this url and replace with /

http:/mysite.appspot.com/_ah/upload /?qa=ask&cat=2/ AMmfu6annb0ziCG7zXsrvLvZLzLsAh-nIitRGGfcxpcNoMTQBvsnxULJ_B5n932EDV5_jd-rllvnSfI5cqPpRUJ8FG6G17J7PHncolJ09PTu_QSLCY6JoVWc9vnmK0Pubx0Xe0OFXepcaXzXJAT1zjShFUFSfEMcCxC5yleRlo7ax2IdRVrPWHHdIqs8XvoOvmNT_7MhnOM4/ALBNUaYAAAAAUy-qo2Nb0qSl-OCR3pifnmzY7HpSBoAt

So that the new url looks like below:

http:/mysite.appspot.com/_ah/upload/AMmfu6annb0ziCG7zXsrvLvZLzLsAh-nIitRGGfcxpcNoMTQBvsnxULJ_B5n932EDV5_jd-rllvnSfI5cqPpRUJ8FG6G17J7PHncolJ09PTu_QSLCY6JoVWc9vnmK0Pubx0Xe0OFXepcaXzXJAT1zjShFUFSfEMcCxC5yleRlo7ax2IdRVrPWHHdIqs8XvoOvmNT_7MhnOM4/ALBNUaYAAAAAUy-qo2Nb0qSl-OCR3pifnmzY7HpSBoAt

I guess I can use preg_replace ($pattern,$replacement,$subject) function where $subject and $replacement will be the original url and / character respectively.

But I can not figure out the $pattern expression.

Please help.

If it is always going to be /?qa=ask&cat=2/ use str_replace() . If it is going to be by position (eg after the question mark and before the the next slash) that is a different matter.

This is the pattern.

$pattern = "?qa=ask&cat=2/";

Here's the documentation. http://www.php.net/preg_replace

As soon as you find /? , replace all until the next / is found:

preg_replace('~/?[^/]+/~', '/', $str);

Please see this https://eval.in/125432

$result = preg_replace ("/\?qa=ask&cat=\d+\//","",$url);

echo $result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM