rbragg Posted October 29, 2007 Share Posted October 29, 2007 Is it possible to use substr() to get only the letters displayed after numbers (at the end of a string)? For example: WA-0318a would give me a SAN-2011Aa would give me Aa KE-2200Eb would give me Eb So, if $variable = "SAN-2011Aa" then substr($variable, -?). The negative length parameter would be dependent on how many of the ending characters are letters and not numbers. In this case, I would need to dynamically generate 2 (for -2). In the case of "WA-0318a" I would need to dynamically generate 1 (for -1). Is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/ Share on other sites More sharing options...
effigy Posted October 29, 2007 Share Posted October 29, 2007 Regular expressions will be the easiest approach: <pre> <?php $tests = array( 'WA-0318a', 'SAN-2011Aa', 'KE-2200Eb' ); foreach ($tests as $test) { echo "<b>$test</b><br>"; preg_match('/(?<=\d)[a-z]+\z/i', $test, $matches); print_r($matches); echo '<hr>'; } ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-380630 Share on other sites More sharing options...
rbragg Posted October 29, 2007 Author Share Posted October 29, 2007 Thanks for your reply. I will only have one string in $variable so I wouldn't start with an array. Also, I wouldn't need ALL of the letters from the string ... only those at the end. <?php preg_match('/(?<=\d)[a-z]+\z/i', $variable, $result); echo $result; ?> This gives me an array of letters. I would like to end up with one string from $variable. In the case of "SAN-2011Aa", I'd like to get "Aa" ... not "A" and "a". Also, I'd like to leave out "SAN". Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-380638 Share on other sites More sharing options...
effigy Posted October 29, 2007 Share Posted October 29, 2007 <pre> <?php $data = <<<DATA WA-0318a SAN-2011Aa KE-2200Eb DATA; preg_match_all('/(?<=\d)[a-z]+(?=(?:\r?\n)|\z)/i', $data, $matches); print_r($matches); echo implode('', $matches[0]); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-380661 Share on other sites More sharing options...
rbragg Posted October 30, 2007 Author Share Posted October 30, 2007 I have: <?php $hallRoomSpace = ociresult($parsedQuery,"SPACE"); preg_match_all('/(?<=\d)[a-z]+(?=(?:\r?\n)|\z)/i', $hallRoomSpace, $matches); print_r($matches); echo implode('', $matches[0]); ?> This echoes: Array ( [0] => Array ( ) ) Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-381451 Share on other sites More sharing options...
effigy Posted October 30, 2007 Share Posted October 30, 2007 What's in $hallRoomSpace? Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-381475 Share on other sites More sharing options...
rbragg Posted October 31, 2007 Author Share Posted October 31, 2007 one example: KE-2200Eb Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-381606 Share on other sites More sharing options...
effigy Posted October 31, 2007 Share Posted October 31, 2007 If I directly assign that, it works: <pre> <?php $hallRoomSpace = 'KE-2200Eb'; preg_match_all('/(?<=\d)[a-z]+(?=(?:\r?\n)|\z)/i', $hallRoomSpace, $matches); print_r($matches); echo implode('', $matches[0]); ?> </pre> And if you're only going to have one sequence in there, you can switch to preg_match. Quote Link to comment https://forums.phpfreaks.com/topic/75256-substr-with-dependent-length-parameter/#findComment-381646 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.