Jump to content

String Searching Help Needed


rwallin

Recommended Posts

Having problems getting consistent search results when searching a string.

 

Have used strstr, stristr, strpos, ereg.., preg.., etc

 

Here's my last line of code I've tried although all the various string search functions i've tried has led to the same results

$found = strpos($myarray[$i],"$title");

 

$title can have one or more words

 

Sometimes it matches sometimes it doesn't

 

for example

'DIXIE LULLABY' is found in  'WHAT BEAUTIFUL LIFE JUST LIKE DIXIE LULLABY'

'STORY' is found in 'THESE LINES ACROSS FACE TELL STORY'

 

BUT

 

'THAT SCARES' is NOT found in 'LIFE WITHOUT WIFE WOULD THAT SCARES'

'TOLD' is NOT found in 'WON'T TOLD'

 

also there are times when the $title has characters that may need to be escaped (') and sometimes when the $title is found in the middle of the search string.

 

any ideas on what I need to do in order to get consistent search results?

Thanks!

 

Link to comment
Share on other sites

here is all the code that I am using the function in to help debug it easier

for ($i=0; $i<$k-1; $i++)
     {
       $title = substr($myarray[0], 10);
for ($j=$i+1; $j<$k; $j++)

{
	if (($txt = substr($myarray[$i], 10)) == substr($myarray[$j], 10))
	{ 
		$found = strpos($txt,"$title");
		echo "$found";

		$q = $q+1;
			if ($q > 1){
			echo $title;
			break;
			}
	}
}
}

Link to comment
Share on other sites

maybe this will help illustrate it more

 

Also the files I am reading in have a time stamp between brackets at the beginning of each line [00:00.00] that I am stripping out. And I can take the problem lines individual and the search works fine.

I.E.

$String = "[01:31.69]HAVE CHANGED THINGS STARTIN' WITH";
$Search = "STARTIN' WITH";

$Result = strpos($String,"$Search");



echo $Result;

 

<?php
echo "Testing String Search<br><Br>";
/*** Read All Files In Directory ***/
$directory = "output2";	// Directory name that contains lrc files

if ($handle = opendir($directory)) {


while (false !== ($file = readdir($handle))) {

$myarray = file("$directory/$file");
$k = count($myarray);
for ($i=0; $i<$k-1; $i++)
						{
// Array Key 0 will always contain title
$title = substr($myarray[0], 10);


for ($j=$i+1; $j<$k; $j++)
{
	if (($txt = substr($myarray[$i], 10)) == substr($myarray[$j], 10))
	{ 
		$found = strpos($txt,"$title");
		if ($found === FALSE){
		echo "$title <b>NOT</b> FOUND in $txt<br>";
		}else{
		echo "$title <b>FOUND</b> starting at key <b>$found</b> in $txt<br>";
		}

		$q = $q+1;
			if ($q > 1){
			//echo $title;
			break;
			}
	}
}


	}
}
}

?>

 

 

Results:

Testing String Search

 

MAKES WONDER NOT FOUND in STILL DON'T HAVE REASON DON'T HAVE TIME

MAKES WONDER NOT FOUND in REALLY MAKES WONDER EVER

MAKES WONDER NOT FOUND in GAVE F### ABOUT

MAKES WONDER NOT FOUND in GIVE SOMETHIN' BELIEVE 'CAUSE

MAKES WONDER NOT FOUND in DON'T BELIEVE ANYMORE ANYMORE

MAKES WONDER NOT FOUND in WONDER EVEN MAKES DIFFERENCE

MAKES WONDER NOT FOUND in GIVE SOMETHIN' BELIEVE 'CAUSE

MAKES WONDER NOT FOUND in DON'T BELIEVE ANYMORE ANYMORE

MAKES WONDER NOT FOUND in WONDER EVEN MAKES DIFFERENCE EVEN MAKES

MAKES WONDER NOT FOUND in DIFFERENCE YEAH

MAKES WONDER NOT FOUND in TOLD YOU'RE FEELIN' DON'T

MAKES WONDER NOT FOUND in BELIEVE IT'S TRUE ANYMORE ANYMORE

MAKES WONDER NOT FOUND in THIS GOODBYE

MAKES WONDER NOT FOUND in GOODBYE

SMILE NOT FOUND in LOST BACK THEN WITH LITTLE HELP

SMILE NOT FOUND in FROM FRIENDS FOUND LIGHT TUNNEL

SMILE NOT FOUND in YOU'RE CALLIN' PHONE HAVE

SMILE NOT FOUND in LITTLE WHINE MOAN IT'S ONLY BECAUSE

SMILE NOT FOUND in YOU'RE FEELIN' ALONE

SMILE FOUND starting at key 22 in FIRST WHEN YEAH MAKES SMILE

SMILE FOUND starting at key 11 in YEAH MAKES SMILE

SMILE FOUND starting at key 27 in WORST FEEL WHILE THEN JUST SMILE

SMILE FOUND starting at key 6 in AHEAD SMILE

SMILE FOUND starting at key 6 in AHEAD SMILE

THNKS MMRS NOT FOUND in NIGHT MORE TIME THANKS MEMORIES

THNKS MMRS NOT FOUND in EVEN THOUGH THEY WEREN'T GREAT

THNKS MMRS NOT FOUND in TASTES LIKE ONLY SWEETER

THNKS MMRS NOT FOUND in NIGHT YEAH MORE TIME THANKS MEMORIES

THNKS MMRS NOT FOUND in THANKS MEMORIES

THNKS MMRS NOT FOUND in TASTES LIKE ONLY SWEETER

THNKS MMRS NOT FOUND in NIGHT MORE TIME THANKS MEMORIES

THNKS MMRS NOT FOUND in EVEN THOUGH THEY WEREN'T GREAT

THNKS MMRS NOT FOUND in TASTES LIKE ONLY SWEETER

THNKS MMRS NOT FOUND in THANKS MEMORIES

THNKS MMRS NOT FOUND in TASTES LIKE ONLY SWEETER

THNKS MMRS NOT FOUND in TASTES LIKE ONLY SWEETER

THNKS MMRS NOT FOUND in TASTES LIKE ONLY SWEETER

STARTIN' WITH NOT FOUND in DIME HALF THINGS THAT DIDN'T

STARTIN' WITH NOT FOUND in ONLY HAVE KNOWN THAT LATER DOWN ROAD

STARTIN' WITH NOT FOUND in LOOK BACK LIKE WHAT

STARTIN' WITH NOT FOUND in HAVE CHANGED THINGS STARTIN' WITH

STARTIN' WITH NOT FOUND in ONLY HAVE KNOWN THAT LATER DOWN ROAD

STARTIN' WITH NOT FOUND in LOOK BACK LIKE WHAT

BOYS NOT FOUND in KNOW DON'T MUCH LIKE LADY STILL NEED

BOYS NOT FOUND in SOMEBODY'S BABY

BOYS NOT FOUND in YEAH MIGHT FIND MAKIN' MUCH NOISE

BOYS FOUND starting at key 15 in MORE THAN JUST BOYS

BOYS NOT FOUND in YEAH MORE THAN JUST BOYS

BABY LOVE NOT FOUND in COME BABY BABY BABY LOVE ONLY

DIXIE LULLABY FOUND starting at key 30 in WHAT BEAUTIFUL LIFE JUST LIKE DIXIE LULLABY

DIXIE LULLABY FOUND starting at key 30 in WHAT BEAUTIFUL LIFE JUST LIKE DIXIE LULLABY

DIXIE LULLABY FOUND starting at key 30 in WHAT BEAUTIFUL LIFE JUST LIKE DIXIE LULLABY

LITTLE MORE NOT FOUND in COME BABY GIVE LITTLE MORE COME BABY

LITTLE MORE NOT FOUND in GIVE LITTLE MORE

LITTLE MORE NOT FOUND in COME BABY GIVE LITTLE MORE COME BABY

LITTLE MORE NOT FOUND in GIVE LITTLE MORE

LITTLE MORE NOT FOUND in HERE FALLIN' HEART'S CALLIN'

LITTLE MORE NOT FOUND in KNOW NEVER ENOUGH DON'T HOLD BACK

LITTLE MORE NOT FOUND in YOUR LOVE

LITTLE MORE NOT FOUND in STRONG STEADY READY COME BABY

LITTLE MORE NOT FOUND in GIVE LITTLE MORE

LITTLE MORE NOT FOUND in GIVE LITTLE MORE

TOLD NOT FOUND in CAN'T THAT WORSE BETTER WE'RE BETTER

TOLD NOT FOUND in TOGETHER PLEASE

TOLD NOT FOUND in JUST COME BACK HOME DON'T THAT YOU'RE SORRY

TOLD NOT FOUND in WON'T TOLD

TOLD NOT FOUND in CAN'T THAT WORSE BETTER WE'RE BETTER

TOLD NOT FOUND in TOGETHER PLEASE

TOLD NOT FOUND in JUST COME BACK HOME DON'T THAT YOU'RE SORRY

TOLD NOT FOUND in WON'T TOLD

TOLD NOT FOUND in JUST COME BACK HOME DON'T THAT YOU'RE SORRY

STORY FOUND starting at key 29 in THESE LINES ACROSS FACE TELL STORY

STORY NOT FOUND in WHERE

STORY NOT FOUND in THESE STORIES DON'T MEAN ANYTHING WHEN YOU'VE

STORY NOT FOUND in TELL THEM IT'S TRUE MADE

THAT SCARES NOT FOUND in STARTS ASKIN' ABOUT HISTORY

THAT SCARES NOT FOUND in DRUGS ROCK ROLL

THAT SCARES NOT FOUND in LIFE WITHOUT WIFE WOULD THAT SCARES

 

 

 

 

Link to comment
Share on other sites

hmm

This is the example they give on that link

 

$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);   // strpos(haystack, needle)

 

the $txt variable is my "haystack ($mystring)" and the $title variable is my "needle" ($findme)

I want to find $title in $txt

 

so wouldn't

$found = strpos($title, $txt);

be backwards?

 

If I do $found = strpos($title, $txt); then everything comes back saying that nothing was found.

 

 

 

 

 

 

Link to comment
Share on other sites

ok ..

I ended up setting new variables for my 'Haystack' and 'Needle' and removed all characters except letters and numbers and it seems to work

		$newTxt = ereg_replace("[^A-Za-z0-9]", "", $txt);
		$newTitle = ereg_replace("[^A-Za-z0-9]", "", $title);

 

but I would still love to know why it would not work 100% of the time before

Link to comment
Share on other sites

Ok, then there there may be something going on with how the data is read or manipulated. It would be very helpful if you provide one of the files you are processing. I made a wrorking prototype from the data you have above and it works. But, I am thinking a possibility is that a "space" in one string might be a different "space" than is used in another string.

 

You coud use ord() to find the character code used for the characters in each string.

 

EDIT: yeah, your last post suggests that there may be a difference between the "space" characters since it works when you remove the non alphanumeric characters

Link to comment
Share on other sites

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.