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? Link to comment https://forums.phpfreaks.com/topic/56127-preg_place-reg-expression-help/ Share on other sites More sharing options...
effigy Posted June 19, 2007 Share Posted June 19, 2007 #/q\?s=([A-Z]{3,5})# Link to comment https://forums.phpfreaks.com/topic/56127-preg_place-reg-expression-help/#findComment-277372 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. Link to comment https://forums.phpfreaks.com/topic/56127-preg_place-reg-expression-help/#findComment-277405 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> Link to comment https://forums.phpfreaks.com/topic/56127-preg_place-reg-expression-help/#findComment-277615 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.