简体   繁体   中英

The search results in Drupal includes HTML Entities. How can I have a clean output?

How can I have a clean html ouput for search result pages? Each time I try to include special characters like "&" as part of the search term, I usually get results with "&" highlighted yet includes the HTML entity. Thus, the results has &, " etc...Here's a screenshot sample - http://min.us/mt3rOV5zVtOh6

Meanwhile, when I do my searches with "&" included in the search term, the result yields to having a clean output.

The piece of code in search-result.tpl.php

http://pastebin.com/zCmMJLNh

I've already tried several decoding functions but no success. Been trying to fix this for days already. The site is using Drupal 6 and the search module has been overridden.

You say "...the search module has been overridden" this could be the cause of why the search snippet remains htmlentityencoded on output ( eg check_plain'd escaped html )

A better fix would be to find the cause in the modification, eg a preprocess function that modifies the search snippet ( if any )

Alternatively, you could probably run the $snippet through decode_entities

ie print decode_entities($snippet)

Assuming, the html is already escaped, as if not, can be a security risk.

See also: http://php.net/manual/en/function.html-entity-decode.php and: http://www.php.net/manual/en/function.htmlspecialchars-decode.php

Well, you could try drupal_html_to_text to convert the snippet into plain text.

The right way is probably to figure out why those results aren't getting converted. Based on your comments it looks like the problem is only when you search specifically for "&". More specifically, it's the regex in the search.module (/modules/search/search.module - line 1188 in 6):

preg_match_all('/ ("([^"]+)"|(?!OR)([^" ]+))/', ' '. $keys, $matches);

It only matches spaces before the keyword (not after). You could modify the $keys here like:

if ($keys == '&') $keys = '&'

Or something like that (of course that means hacking core - meh).

You could also possibly add a form_alter via a module and modify the search form (see this link on how to add the form_alter ). Then you could add a custom submit handler which would alter the search term in the form before it is submitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM