Jump to content


This topic is now archived and is closed to further replies.


Need help with stristr and mysql result

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.


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



Share this post

Link to post
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!


Share this post

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

Share this post

Link to post
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.

Share this post

Link to post
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

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?

Share this post

Link to post
Share on other sites


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.