Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
363 views
in Q2A Core by
I want to put "C++" and ".NET" as tags, but Q2A seems to otomatiqually convert them to "C" and "NET" respectively. How can I fix this issue ?
Q2A version: 1.7

1 Answer

+2 votes
by
selected by
 
Best answer

There are a few approaches to tackle this issue. One would be to enable the Use comma as the only tag separator setting in admin/posting. This way you would be able to input anything but commas, slashes or backslashes (might depend on your web server but Apache has issues with the slashes on Linux).

The other alternative, is the one Gideon suggests in his answer here. The thing with this approach is that it will impact many other parts of the core that require splitting words and you might not want that to happen.

The last alternative would be just to customize the function that processes the tags itself. That way you'll know you'd only be changing how tags are processed without impacting the rest of the core. In order to do so, follow these steps:

1. Edit the qa-include/app/format.php file

2. Locate the qa_get_tags_field_value function in this line

3. Edit it so that it looks this way:

function qa_get_tags_field_value($fieldname) {
    require_once QA_INCLUDE_DIR . 'util/string.php';
    $text = qa_post_text($fieldname);
    if (qa_opt('tag_separator_comma'))
        return array_unique(preg_split('/\s*,\s*/', trim(qa_strtolower(strtr($text, '/', ' '))), -1, PREG_SPLIT_NO_EMPTY));
    else {
        $delimiters = ' "\',[]'; // Add any tag separator
        return array_unique(preg_split('/([\n\r\t\/\\\\' . preg_quote($delimiters, '/') . ']+)/', qa_strtolower($text), -1, PREG_SPLIT_NO_EMPTY));
    }
}

4. Edit the $delimiter variable and add any character there that you want to consider a word breaker. They will not be part of the tags and will make the words to split into 2 tags, when found. Add anything you want but make sure, if you add an apostrophe, to escape it this way \'. Note that apart from the characters added in there, the slash and backslash characters are delimiters that you shouldn't remove from the following line, in order to avoid routing issues.

Example after following the previous steps:

Input tag string: c++ /home/user:/etc/paswd C:\Windows (T@G!) file*.zip "hello" one[again]two.tags

Generated tags:

  1. c++
  2. home
  3. user:
  4. etc
  5. paswd
  6. c:
  7. windows
  8. (t@g!)
  9. file*.zip
  10. hello
  11. one
  12. again
  13. two.tags
by
Hi, thank you very much. I have used the last alternative and it seems to work perfectly.
However, I am not sure about the last line, are you sure there is no security problem (XSS or sql injection) in the last line: return array_unique(preg_split('/([\n\r\t\/\\\\' . preg_quote($delimiters, '/') . ']+)/', qa_strtolower($text), -1, PREG_SPLIT_NO_EMPTY)); ?
...