All4172 Posted June 19, 2007 Share Posted June 19, 2007 What I"m wanting to do is to take: /q?s=ABCDE and to change it to /mydirectory/ABCDE.php Sometimes the file is 3 letters sometimes 4 and other times five. What is the correct regex to use in a preg_replace / str_replace function? Quote Link to comment Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 #/q\?s=([A-Z]{3,5})# Quote Link to comment Share on other sites More sharing options...
All4172 Posted June 19, 2007 Author Share Posted June 19, 2007 That gets me to match it...but how do I phrase it in order to keep the letters though? In the example above I need to somehow keep the ABCDE in order to apend it with the .php on the end. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 The parentheses capture the information into backreference \1. <pre> <?php $str = '/q?s=ABCDE'; echo preg_replace('#/q\?s=([A-Z]{3,5})#', '/mydirectory/\1.php', $str); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.