Catfish Posted August 2, 2009 Share Posted August 2, 2009 Hi all, I am trying to make my script search for a needle and highlight it with <i>needle</i> tags in the string. I tried using str_ireplace for this like: $highlightValue = str_ireplace($dataRecordDate, '<i>'.$dataRecordDate.'</i>', $value); but it is not putting all of the highlighted part into the string. I am only getting "i>" not "<i>dataRecordDate</i>". It doesn't matter if I change the tags to <b></b> either, it's the same except with a b instead of i. If I just use a single text string it works fine (no HTML tags) so this suggests to me the problem may be the / character but I don't know why that would cause problems with str_ireplace. I have also tried the same thing with preg_replace and even used some examples I searched for on this forum but they all come out with the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/168529-solved-search-tag-highlighting/ Share on other sites More sharing options...
wildteen88 Posted August 2, 2009 Share Posted August 2, 2009 The snippet you provided is fine (on its own). We'll need to see more code. Quote Link to comment https://forums.phpfreaks.com/topic/168529-solved-search-tag-highlighting/#findComment-889033 Share on other sites More sharing options...
Catfish Posted August 3, 2009 Author Share Posted August 3, 2009 Here is a small block of the code that the str_ireplace call is made in. I use a number of these code blocks to search based on different criteria, but this one looks for a field call 'financeOppParty' and if it finds a match, it verified that the $value has not already been added to the $arraySearchMatches array(s). There are various levels to arraySearchMatches defining the likeliness that the current $value is the one you would be looking for. "Relevance" would be the best word for it. The rest of the code functions fine (searching for data in the values presented, placing in the relevance levels of the arraySearchMatches array) so I decided to add a feature that it would highlight exactly what it found in the $value string for the user to see why that $value is in the result list, but the thing is not working: // opposite party search (medium accurate) if (preg_match("/".$arrayRecord['financeOppParty']."/i", $value)) { $dataAlreadyFound = 0; // check value isn't already in searchMatches and add accordingly if (array_search($value, $arraySearchMatches)) $dataAlreadyFound = 1; } else $dataAlreadyFound = 1; if ($dataAlreadyFound != 1) { $highlightValue = str_ireplace($arrayRecord['financeOppParty'], '<i>'.$arrayRecord['financeOppParty'].'</i>', $value); $arraySearchMatches[1]['text'][] = $value; $arraySearchMatches[1]['highlights'][] = $highlightValue; } EDIT: i made a small edit in the code how i perform the search in arraySearchMatches from a foreach loop to use array_search function. Quote Link to comment https://forums.phpfreaks.com/topic/168529-solved-search-tag-highlighting/#findComment-889230 Share on other sites More sharing options...
Catfish Posted August 3, 2009 Author Share Posted August 3, 2009 Here is a print_r of arraySearchMatches: Array ( [1] => Array ( [text] => Array ( [0] => ../private/data/catfish/paperwork/business/correspondence/unknown-document-[2008]-nathan-small_business_field_officer_gold_coast_contact_details.comp.tif [1] => ../private/data/catfish/paperwork/business/correspondence/unknown-tax_return-[2004-2005]-carlie-income_tax_return_estimate_200327813.comp.tif ) [highlights] => Array ( [0] => ../private/data/catfish/paperwork/business/correspondence/<i>Unknown</i>-document-[2008]-nathan-small_business_field_officer_gold_coast_contact_details.comp.tif [1] => ../private/data/catfish/paperwork/business/correspondence/<i>Unknown</i>-tax_return-[2004-2005]-carlie-income_tax_return_estimate_200327813.comp.tif ) ) [2] => Array ( [text] => Array ( [0] => ../private/data/catfish/paperwork/business/taxation/receipts/taxation-receipts-[august_2007]-nathan-fuel_dockets_tax_invoice.tif [1] => ../private/data/catfish/paperwork/business/taxation/receipts/taxation-receipts-[august_2007]-nathan-tools_dockets_tax_invoice.tif ) [highlights] => Array ( [0] => ../private/data/catfish/paperwork/business/taxation/receipts/taxation-receipts-[<i>august_2007</i>]-nathan-fuel_dockets_tax_invoice.tif [1] => ../private/data/catfish/paperwork/business/taxation/receipts/taxation-receipts-[<i>august_2007</i>]-nathan-tools_dockets_tax_invoice.tif ) ) [0] => Array ( [text] => Array ( [0] => ../private/data/catfish/paperwork/personal/banks/nab/nab-statement-[20070801]-nathan-visa_wedding_project_load_transaction_history_itemized_listing-1of2.comp.tif [1] => ../private/data/catfish/paperwork/personal/banks/nab/nab-statement-[20070801]-nathan-visa_wedding_project_load_transaction_history_itemized_listing-2of2.comp.tif ) [highlights] => Array ( [0] => ../private/data/catfish/paperwork/personal/banks/nab/nab-statement-[20070801]-nathan-visa_wedding_project_load_transaction_history_itemized_listing-1of2.comp.tif [1] => ../private/data/catfish/paperwork/personal/banks/nab/nab-statement-[20070801]-nathan-visa_wedding_project_load_transaction_history_itemized_listing-2of2.comp.tif ) ) ) which actually shows that the highlighting code IS working, as the values in the [highlights] sub-array are correct. Notice that some sections of those strings have <i>...</i> tags around them. So my problem must be somewhere in the output of those values. Here is the code that outputs the values: ksort($arraySearchMatches); foreach($arraySearchMatches as $findLevel => $matchList) { if ($findLevel == 0) $findString = 'high'; elseif ($findLevel == 1) $findString = 'medium'; elseif ($findLevel == 2) $findString = 'low'; print('<tr>' .'<td class="tableTDAlignLeft" colspan="2">' .'<b>'.ucwords($findString.' match likeliness').'</b>' .'</td></tr>'."\n"); foreach ($matchList['text'] as $key => $value) { print("<tr>" ."<td class=\"tableTDAlignLeft\">" .basename($matchList['highlights'][$key]) ."</td>" ."<td class=\"tableTDAlignCenter\">" ." <a href=\"".$value."\">" ."View</a>" ."|" ."<a href=\"selectAssociatedPaper.php?" ."idnum=".$_GET['idnum']."" ."&financePaperwork=".urlencode($value)."" ."&action=set\">" ."Set</a>" ."</td></tr>\n"); } } the call to basename() is where the highlighted values get used. I call basename because I only want it to output the filename part of the string, not the absolute path. Everything else, afaik, is working in thise script for me. Quote Link to comment https://forums.phpfreaks.com/topic/168529-solved-search-tag-highlighting/#findComment-889239 Share on other sites More sharing options...
Catfish Posted August 3, 2009 Author Share Posted August 3, 2009 just bumping this thread as I am still stuck on why it is not outputting the highlighted data correctly. Quote Link to comment https://forums.phpfreaks.com/topic/168529-solved-search-tag-highlighting/#findComment-889910 Share on other sites More sharing options...
Catfish Posted August 5, 2009 Author Share Posted August 5, 2009 I have solved this issue! The problem was nothing to do with preg_replace or str_ireplace, they were working perfectly as I expected. The problem was occuring with the call to basename() in my print() calls. Because the path to the filename is: /something/else/filename-blah-blah.ext and I search for "filename" for example, i was highlighting the search term and storing the "highlighted" value in another array (because I need the original and the highlighted filename for output). The highlighting code inserts <i> </i> tags, which introduces a new slash character to the string, which then confuses the output that basename() gives since it removes the LHS of the string up to the last slash in the string! so if I was giving basename: /something/else/<i>filename</i>-blah-blah.ext it sees the last / in the </i> tag as the start of the base name and gives: i>-blah-blah.ext to the output. I have overcome the problem by using the following preg_replace call in my print() calls: <?php print(preg_replace('/(.*)?<i>/U', '<i>$2', $matchList['highlights'][$key], 1)); ?> which removes anything up to (and including) the first occurence of <i> with "<i>" and anything to RHS of <i> in the original string. Quote Link to comment https://forums.phpfreaks.com/topic/168529-solved-search-tag-highlighting/#findComment-891563 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.