String (needle) upper and lower case letters are different.
Last active
April 13, 2018 23:02
-
-
Save lenivene/d19fabdfda4f2a390193ff8ac9d22697 to your computer and use it in GitHub Desktop.
Checa se um valor existe em uma string
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
<?php | |
function in_string( $needle, $haystack ){ | |
if( !isset( $needle ) || isset( $needle ) && !is_string( $needle ) ){ | |
$error['needle'] = 'Param $needle invalid'; | |
} | |
if( !isset( $haystack ) || isset( $haystack ) && !is_string( $haystack ) ){ | |
$error['$haystack'] = 'Param $needle invalid'; | |
} | |
if( isset( $error ) ){ | |
trigger_error( $error, E_USER_ERROR ); | |
} | |
$needle = str_replace( array( | |
'$', '/', "'", '"', '^', '*', '+', '-', '{', '}', '(', ')', | |
',', '.', '?', '!', '@', '#', '&', '[', ']', ';', '<', '>', | |
'`', '´', '~', '%', '-', '=', '|' | |
), array( | |
'\$', '\/', "'", '"', '\^', '\*', '\+', '\-', '\{', '\}', '\(', '\)', | |
'\,', '\.', '\?', '\!', '\@', '\#', '\&', '\[', '\]', '\;', '\<', '\>', | |
'\`', '\´', '\~', '\%', '\-', '\=', '\|' | |
), strip_tags( $needle ) ); | |
$in_string = array_filter( preg_split( "/$needle/", $haystack ) ); | |
if( empty( $in_string ) ){ | |
return true; | |
} | |
return false; | |
} | |
$haystack = 'ababcd'; | |
$needle = 'ababcd'; | |
var_dump( in_string( $needle, $haystack ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment