Jump to content

Help - Find String within String between Pipes ||


Recommended Posts

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

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';
}
?>

 

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";
}

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.