Jump to content


Photo

Need help with stristr and mysql result


  • Please log in to reply
5 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 11 October 2006 - 02:35 AM

I have the following code that searches a the database a  reserved trackid if its found in the trackid supplied its replaced with *** the problem is this:

say that the database has both string and stringer as reserved trackids, and the trackid entered is stringer so far the result would be ******er. my question is how do i get it to return ******** instead.


while ($row = mysql_fetch_array($reserved)) {
	$reservation = $row[reserved];
	
          if (stristr(trim($trackid),$reservation))	{

     
                $length = strlen($reservation);
            for ($i = 1; $i <= $length; $i++) {
                $stars .= "*";
            }

            $newreservation = eregi_replace($reservation,$stars,trim($trackid));
            $stars = "";

        }
    }





#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 11 October 2006 - 09:57 AM

I don't totally understand what your doing, but I would guess you should be doing this in your database query!

SELECT trackid, REPEAT('*', LENGTH(reserved)) AS reservation FROM table WHERE ...;

The above is just simple example, but you can add other conditions so you can get the exact result you want right in your query, which will always be faster than what you might write in your PHP scipting!


me!

#3 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 11 October 2006 - 10:22 PM

i get what your sayin but it wont work for words like stringerboydstringg and plus i need it to catch multiple reserved trackid in the same user input eg ********boyd*****g

#4 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 12 October 2006 - 01:56 AM

Correct me if I'm wrong in my understanding here:

- You have strings such as "stringerstringed".
- You want to search for strings within these strings.  For example, you want to search for "string" and "stringed".
- You want to take the longest match in every case.  That is, you prefer "stringed" over "string".  Is this assumption correct?
- Any character can appear immediately before and after trackids.  Is this correct?

Once that's clarified we can look at solutions.

#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 12 October 2006 - 03:45 AM

You hit the nail on the head. Do you have any ideas

#6 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 12 October 2006 - 06:22 AM

The most straightforward solution I can think of is to sort the trackids from longest to shortest.  Then check them in that order.  That guarantees finding the longest match first.  Something like:

function length_cmp($a, $b) {
  $a_len = strlen($a);
  $b_len = strlen($b);
  if ($a_len < $b_len) return -1;
  if ($a_len > $b_len) return 1;
  return 0;
}
$trackids = array('string', 'stringed', 'stringest');
usort($trackids, 'length_cmp');
foreach ($trackids as $trackid) {
  # Check this one and replace if found
}

If the sort order is wrong, replace -1 with 1 and 1 with -1.  I don't remember which way is correct :)

Does that help?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users