Skip to content

Instantly share code, notes, and snippets.

@giovanemachado
Last active August 26, 2019 16:25
Show Gist options
  • Save giovanemachado/e1fed3275d0da2b897a285f3ccb47a04 to your computer and use it in GitHub Desktop.
Save giovanemachado/e1fed3275d0da2b897a285f3ccb47a04 to your computer and use it in GitHub Desktop.
Checa se há numa string uma determinada palavra, entre outras duas. Por exemplo, se forem dadas como $start = 'A' e como $end = 'B', a string 'AXYZB' retornaria 'XYZ'. Baseado em uma resposta no stackoverflow e adaptado para evitar problemas com a posição 0 no strpos.
function get_between_word( $string, $start = "", $end = "" ) {
if ( strpos( $string, $start ) !== false ) {
$startCharCount = strpos( $string, $start ) + strlen( $start );
$firstSubStr = substr( $string, $startCharCount, strlen( $string ) );
$endCharCount = strpos( $firstSubStr, $end );
if ( $endCharCount == 0 ) {
$endCharCount = strlen( $firstSubStr );
}
return substr( $firstSubStr, 0, $endCharCount );
} else {
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment