Last active
August 26, 2019 16:25
-
-
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.
This file contains hidden or 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
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