Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created September 2, 2010 23:06
Show Gist options
  • Save wtnabe/563129 to your computer and use it in GitHub Desktop.
Save wtnabe/563129 to your computer and use it in GitHub Desktop.
PHP's pseudo String#&&
<?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