Domanda

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.

È stato utile?

Soluzione

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

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

echo $result;

Altri suggerimenti

If it is always going to be /?qa=ask&cat=2/ use str_replace(). If it is going to be by position (e.g. 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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top