Skip to content

Instantly share code, notes, and snippets.

@lenivene
Last active April 13, 2018 23:02
Show Gist options
  • Save lenivene/d19fabdfda4f2a390193ff8ac9d22697 to your computer and use it in GitHub Desktop.
Save lenivene/d19fabdfda4f2a390193ff8ac9d22697 to your computer and use it in GitHub Desktop.
Checa se um valor existe em uma string

String (needle) upper and lower case letters are different.

<?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