oracle259 Posted October 11, 2006 Share Posted October 11, 2006 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] Quote Link to comment Share on other sites More sharing options...
printf Posted October 11, 2006 Share Posted October 11, 2006 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! Quote Link to comment Share on other sites More sharing options...
oracle259 Posted October 11, 2006 Author Share Posted October 11, 2006 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 Quote Link to comment Share on other sites More sharing options...
btherl Posted October 12, 2006 Share Posted October 12, 2006 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. Quote Link to comment Share on other sites More sharing options...
oracle259 Posted October 12, 2006 Author Share Posted October 12, 2006 You hit the nail on the head. Do you have any ideas Quote Link to comment Share on other sites More sharing options...
btherl Posted October 12, 2006 Share Posted October 12, 2006 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.