Jump to content

Need help with stristr and mysql result


oracle259

Recommended Posts

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.

[code]

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

        }
    }



[/code]
Link to comment
Share on other sites

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

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

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!
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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:

[code]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
}[/code]

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?
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.