Re: Funky array question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

what have you been trying for comparison so far that has not been working?

if you have php 5, you would want to use stripos, which is case- INsensitive, not strpos, which is the opposite. Also, note the warning on the man page. you would want to use a triple equals sign "!===false" because stripos could return a zero or an empty string which is actually "==false".

http://www.php.net/stripos/

if you do not have php 5, i would use regex so you can get case insensitivity.

Jordan



On Oct 16, 2005, at 11:18 PM, Minuk Choi wrote:

Assuming that you are also accepting partial matches... (e.g. if you had a sentence with something like, "Mr. LeBlue says hello", and
that should be a match for "blue")

If $mysqlString contains 1 sentence from the mySQL database(I assume you've a loop or something that'll fetch 1 string per iteration)

$resultStr = $mysqlString;

$bFound=false;
foreach ($myArray as $colorArray)
{
   $firstTerm = $colorArray[0];
   $secondTerm = $colorArray[1];

   if (strpos($resultStr, $firstTerm)!==false)
   {
       $resultStr = $secondTerm;
      $bFound=true;
   }
     if ($bFound)
   break;
}

$mysqlString = $resultStr;


Try that.




Brian Dunning wrote:


I want to create an array like this:

$myArray=array(
    array('orange','A very bright color'),
    array('blue','A nice color like the ocean'),
    array('yellow','Very bright like the sun') ...etc...
)

Sort of like a small database in memory. Then I want to compare each of the rows coming out of a MySQL call, which are sentences, against this array to see if the FIRST TERM in each array element is present in the sentence, and then display the SECOND TERM from the array for each sentence. Make sense? So for these two sentences, and the above array, here's how I want it to output:

"He used blue paint" - A nice color like the ocean.
"The flower was yellow" - Very bright like the sun.

Can someone help me out with the code needed to search the sentence to which FIRST TERM appears in it, and retrieve the SECOND TERM? I've tried too many things and now my brain is tied in a knot.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux