Created
September 2, 2010 23:06
-
-
Save wtnabe/563129 to your computer and use it in GitHub Desktop.
PHP's pseudo String#&&
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* return string from string A AND string B | |
* | |
* Similar Ruby's String#&&, but different. Ruby's behaves character | |
* by character. | |
* | |
* @since 2010-09-01 | |
* @param string $str1 | |
* @param string $str2 | |
* @return string | |
*/ | |
function str_and( $str1, $str2 ) { | |
if ( strlen( $str1 ) >= strlen( $str2 ) ) { | |
$long = $str1; | |
$short = $str2; | |
} else { | |
$long = $str2; | |
$short = $str1; | |
} | |
if ( ($pos = strpos( $long, $short )) !== false ) { | |
return substr( $long, $pos, strlen( $short ) ); | |
} else { | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment