SangrelX Posted January 30, 2011 Share Posted January 30, 2011 Ok... I had typed this post out ONCE already and when I clicked REFRESH IMAGE to get a diff captcha it ERASED MY POST LMAO this is not my night.... What I need help with is probably more simple then I can even think right now - ive been digging at this for 3 hrs now and im out of time for the night I have a DB Record storing ID's between PIPES | when the initial entry is made in DB it stores it like so |47| NOTE: the number could be different these are ID's number doesnt matter its just between Pipes When the second entry is added its added like so |47||67| say we have a total of 5 Entries |47||67||82||55||69| I need to find ID 82 in that string and it has to be between Pipes Find 82 in data between | and return that ID 82 I am putting between pipes because the ID's can be duplicate digits in different lengths so say I have 8 as my ID and down the string i have another id as 88 -- I cant possibly find the correct ID without some sort of seperation character so i used Pipes soo my end goal is the ability to search and if true or false do action if ($result == $find_id){ echo "ID is there"; }else{ echo "NOT THERE -- Adding it"; } Any help is appreciated guys Thanks SangrelX Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/ Share on other sites More sharing options...
sasa Posted January 30, 2011 Share Posted January 30, 2011 try <?php $test = '|47||67||82||55||69|'; $find_id = 47; if (preg_match('/\|'.$find_id.'\|/', $test)){ echo "ID is there"; }else{ echo "NOT THERE -- Adding it"; } ?> Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/#findComment-1167245 Share on other sites More sharing options...
ngreenwood6 Posted January 30, 2011 Share Posted January 30, 2011 I would go for a more simple solution based on what you need and also possibly a faster solution. Regular expressions tend to be slower so this may work faster for you / be easier to understand <?php //the main number to get $num = 82; //the string to compare $string = '|47||67||82||55||69|'; //remove the extra characters from the beginning and end and put the string as an array $compare = explode('||',substr($string,1,strlen($string)-2)); //compare it if(in_array($num,$compare)){ echo 'its there'; } else { echo 'not there'; } ?> Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/#findComment-1167248 Share on other sites More sharing options...
litebearer Posted January 30, 2011 Share Posted January 30, 2011 You said the id's were in a DB, if so then... $needle = "|88|"; $query = "SELECT id FROM tablename WHERE id LIKE '$needle'"; $result = mysql_query($query); $num_rows = mysql_num_rows($result); if($num_rows<1) echo "not found"; }else( echo "Found"; } Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/#findComment-1167288 Share on other sites More sharing options...
SangrelX Posted January 31, 2011 Author Share Posted January 31, 2011 I would go for a more simple solution based on what you need and also possibly a faster solution. Regular expressions tend to be slower so this may work faster for you / be easier to understand <?php //the main number to get $num = 82; //the string to compare $string = '|47||67||82||55||69|'; //remove the extra characters from the beginning and end and put the string as an array $compare = explode('||',substr($string,1,strlen($string)-2)); //compare it if(in_array($num,$compare)){ echo 'its there'; } else { echo 'not there'; } ?> i almost cried when this worked LMAO.... i had been pounding my head on the keyboard trying to understand why last night i couldnt get a function this simple to go Thank you.... as for doing the DB Search I didnt wanna do it to minimize hits on the DB. this way i only access the DB 1 time per call to this page and that to either ADD the ID or Update the IDs in the record Thank u guys for all your help.. thats why I love these forums LOL Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/#findComment-1167590 Share on other sites More sharing options...
kenrbnsn Posted January 31, 2011 Share Posted January 31, 2011 I hope you can stand one more solution... :-) <?php //the main number to get $num = 82; //the string to compare $string = '|47||67||82||55||69|'; $ary = array_filter(explode('|',$string)); if(in_array($num,$ary)){ echo "it's there"; } else { echo 'not there'; } ?> The array_filter function will remove any empty values from the array. Ken Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/#findComment-1167594 Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2011 Share Posted January 31, 2011 Or, if you change your delimiter, you can do this directly in a query - http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set Link to comment https://forums.phpfreaks.com/topic/226115-help-find-string-within-string-between-pipes/#findComment-1167598 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.