Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Highlight search results in html content


boxtel

Recommended Posts

OK one thing I found with this:

 

function highlight_text ($text, $token, $class) {
$text = str_replace('\"', '"', substr(preg_replace('#(\>(((?>([^><]+|(?R)))*)\<))#se', "preg_replace('#\b(" . $token . ")\b#i', '<span class=\"" . $class . "\">\\\\1</span>', '\\0')", '>' . $text . '<'), 1, -1));
return $text;
}

 

If the token keywords dont have spaces around it then it doesnt add in the SPAN tags.

 

For example on my site I have a product name:

 

Apexi GT Spec Intercooler Kit - Nissan 200SX S14/S15 (SR20DET)

 

When I do:

 

product_info.php?products_id=457&keywords=sr20&hl=y

 

It doesn't hilight the SR20 in the product name.

 

 

It only works when the keyword has spaces around it.

 

 

 

Can you fix the function?

Dan

Link to comment
Share on other sites

  • Replies 62
  • Created
  • Last Reply

Looks like you're using her "better" function from post #2, which is better, but....if you replaced that function with the one she gave in post #1, it will highlight whole words AND parts of words.

 

That first pair of functions in post #1 would result in SR20 being highlighted, regardless of whether or not it's followed by the DET part.

 

Personally, I didn't like that, so....that's why I went with the function from post #2. That one will not highlight parts of words. That's why it will only highlight when searching for SR20DET (the whole word) and not pick up/highlight SR20 only. Her first set of functions will, though.

 

I still haven't been able to figure out how to get this to work with apostrophes in search. I sell back issues of magazines and my buyers are collecting and searching for the names of their favorite actors. If the actor has an apostrophe in their name...forget it.

 

Won't work in my test site or my live site. I have one magazine in the test site that contains an actress named Patti D'Arbanville. If I go into Advanced Search and search thru titles and descriptions with her name...I get this:

 

Parse error: syntax error, unexpected T_STRING in /home/julian99/public_html/test/includes/functions/general.php(58) : regexp code on line 1

Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: preg_replace('#\b(patti d'arbanville)\b#i', '<span class="searchTag">\1</span>', '>2-23-93 Soap Opera Magazine PETER BERGMAN<') in /home/julian99/public_html/test/includes/functions/general.php on line 58

 

Since Boxtel appears to have flown the coop, can anyone else diagnose this and fix it? Are you having any trouble searching with apostrophes in words, Dan?

Link to comment
Share on other sites

Looks like you're using her "better" function from post #2, which is better, but....if you replaced that function with the one she gave in post #1, it will highlight whole words AND parts of words.

 

That first pair of functions in post #1 would result in SR20 being highlighted, regardless of whether or not it's followed by the DET part.

 

Personally, I didn't like that, so....that's why I went with the function from post #2. That one will not highlight parts of words. That's why it will only highlight when searching for SR20DET (the whole word) and not pick up/highlight SR20 only. Her first set of functions will, though.

 

I still haven't been able to figure out how to get this to work with apostrophes in search. I sell back issues of magazines and my buyers are collecting and searching for the names of their favorite actors. If the actor has an apostrophe in their name...forget it.

 

Won't work in my test site or my live site. I have one magazine in the test site that contains an actress named Patti D'Arbanville. If I go into Advanced Search and search thru titles and descriptions with her name...I get this:

 

Parse error: syntax error, unexpected T_STRING in /home/julian99/public_html/test/includes/functions/general.php(58) : regexp code on line 1

Fatal error: preg_replace() [<a href='function.preg-replace'>function.preg-replace</a>]: Failed evaluating code: preg_replace('#\b(patti d'arbanville)\b#i', '<span class="searchTag">\1</span>', '>2-23-93 Soap Opera Magazine PETER BERGMAN<') in /home/julian99/public_html/test/includes/functions/general.php on line 58

 

Since Boxtel appears to have flown the coop, can anyone else diagnose this and fix it? Are you having any trouble searching with apostrophes in words, Dan?

 

flown the coop?

 

suggest you excape single quotes in your keywords.

 

as in search for "Patti D\'Arbanville" and you shall find.

 

so if someone searches for "Patti D'Arbanville" you make sure you escape that single quote first.

Treasurer MFC

Link to comment
Share on other sites

Hello, again. :)

 

How do I escape the apostrophes? I mean...I know to type in Patti D\'Arbanville myself, in order for search to work but...the buyers don't.

 

What do I add to the code and where does it go?

Link to comment
Share on other sites

Hello, again. :)

 

How do I escape the apostrophes? I mean...I know to type in Patti D\'Arbanville myself, in order for search to work but...the buyers don't.

 

What do I add to the code and where does it go?

 

well, you could either do a :

 

$keywords = str_replace("'","\'",$keywords);

 

or a

 

$keywords = addslashes($keywords)

 

but the latter probably also adds slashes to the double quotes so you need to determine whether you should do it before ar after the parsing.

 

 

Basically you have 3 phases in this keyword adventure.

 

1) before parsing

2) during parsing

3) after parsing

 

Before parsing the keywords are just a string that the user entered and which you can manipulate as a string.

 

When parsing, the system evaluates the string and returns an array or the individual search components to be used by the query including the and /or and the ( ) components.

 

After parsing you basically have that array to play around with.

 

When you understand what the parser does and does not do, you are a long way ahead as I do not recommend messing with the spagetti in the parser.

 

example, you can experiment with it yourself.

(presuming and is your default operator)

 

keyword : how are you

parser : [how][and][are][and][you]

 

keyword : "how are you"

parser : [how are you]

 

keyword : how and (are or "you and me")

parser : [how][and][(][are][or][you and me][)]

 

keyword : "how"

parser : ["how"]

 

 

so you see that the parser removes the double quotes when a PHRASE is entered but does not when they only contain a single word. Like it or not, that is the way it is.

 

So if you would like to search for how as a single word, "how" is not the way to do it unless you replace the double quotes with spaces AFTER the parsing. Still even then, if the word how is the first or the last word in the text, it will not be found as the query searches for like '% how %' and will not find the first or last word unless you add spaces at the beginning and end of your text.

 

You can experiment with that parser by simply adding a statement : print_r($search_keywords);

after the parser is called in advanced search result.

 

So some changes are easily made BEFORE the parsing and some are better left AFTER the parsing.

Treasurer MFC

Link to comment
Share on other sites

I cannot read the code, Boxtel. I'm not a programmer. All I can do is follow instructions.

 

Where do you suggest I put that line of code? Which file? Where in the file? :blink:

Link to comment
Share on other sites

I cannot read the code, Boxtel. I'm not a programmer. All I can do is follow instructions.

 

Where do you suggest I put that line of code? Which file? Where in the file? :blink:

 

Well, if you want to manage an osc store which is a little different from the out-of-the-box version yourself, then at least you could/should try to read and understand the code, at least the simple parts.

 

Even if you think you can only follow instructions, sooner or later that will no longer do as most contributions are written for original code and if you no longer have original code after adding 3 or 4 contributions, just following the instructions without understanding them becomes foolish and sometimes pretty risky.

 

in other words, in my opinion, you cannot manage a non-stock osc website if you can only follow instructions.

Treasurer MFC

Link to comment
Share on other sites

95% of the people posting on this website, Boxtel, are just like me.

 

Learning as they go. That's all you should get out of this.

 

I'm sorry that you're frustrated at my inability to read your mind as you post. I can't. And I've made that perfectly clear, I believe.

 

You're the programmer and I'm the one learning.

 

It's kind of like cars. Everyone can drive one, but...how many can fix their engine when it dies? That's what mechanics are for. You're the mechanic here, Boxtel.

 

The OSC software is complicated. It involves a very steep learning curve for anyone who gets involved with it. I agree, that one needs to get heavily involved and learn more about how things really work...especially, the more contribs they attempt to install. I'll give you that all day long. I am trying.

 

2 years ago, all I knew how to do was type fast. I've learned quite a bit over the last couple of years and I'm hardly running some stupid out-of-the-box OSC site.

 

Soapoperaworld dot com if you care to look. Not the best it could be, but....certainly, not the worst OSC site I've seen, either.

 

I ask very simple, straightforward questions of you, Boxtel. Why can't you just answer my questions? You're giving me speeches about parsing that go WHOOSH! right over my head when all I want is an answer to my question.

 

I can appreciate what's happening here, I really can. You're the one who knows everything and you find my questions tedious and silly. I get it. I feel that way sometimes browsing other boards within this site. Been there and conquered that, but...you know what? I have to jerk myself back into reality and remind myself that I asked those same questions long ago, too. In other words, there's no such thing as a stupid question.

 

There's just people patient enough to answer the question or there are...others.

 

All I have left to resolve is this apostophes-in-search problem. That's it. I have keyword highlighting in both titles and descriptions...whether they search with quotation marks or not. I simply have this one little, trivial thing left to fix.

 

I want to fix it because it's not trivial to me, personally. I don't want anyone searching my site with apostrophes in their keywords and seeing what amounts to imcompetence rolling down on their screen in the form of errors.

 

Apostrophes in search is nothing to worry about for most, but...I sell actors. And many of those people have apostrophes in their names. Should be simple enough to fix. I just don't know how.

 

I messed around with your code suggestions in advanced_search_result.php all evening and nothing is working. Am I in the right file?

Link to comment
Share on other sites

95% of the people posting on this website, Boxtel, are just like me.

 

Learning as they go. That's all you should get out of this.

 

I'm sorry that you're frustrated at my inability to read your mind as you post. I can't. And I've made that perfectly clear, I believe.

 

You're the programmer and I'm the one learning.

 

It's kind of like cars. Everyone can drive one, but...how many can fix their engine when it dies? That's what mechanics are for. You're the mechanic here, Boxtel.

 

The OSC software is complicated. It involves a very steep learning curve for anyone who gets involved with it. I agree, that one needs to get heavily involved and learn more about how things really work...especially, the more contribs they attempt to install. I'll give you that all day long. I am trying.

 

2 years ago, all I knew how to do was type fast. I've learned quite a bit over the last couple of years and I'm hardly running some stupid out-of-the-box OSC site.

 

Soapoperaworld dot com if you care to look. Not the best it could be, but....certainly, not the worst OSC site I've seen, either.

 

I ask very simple, straightforward questions of you, Boxtel. Why can't you just answer my questions? You're giving me speeches about parsing that go WHOOSH! right over my head when all I want is an answer to my question.

 

I can appreciate what's happening here, I really can. You're the one who knows everything and you find my questions tedious and silly. I get it. I feel that way sometimes browsing other boards within this site. Been there and conquered that, but...you know what? I have to jerk myself back into reality and remind myself that I asked those same questions long ago, too. In other words, there's no such thing as a stupid question.

 

There's just people patient enough to answer the question or there are...others.

 

All I have left to resolve is this apostophes-in-search problem. That's it. I have keyword highlighting in both titles and descriptions...whether they search with quotation marks or not. I simply have this one little, trivial thing left to fix.

 

I want to fix it because it's not trivial to me, personally. I don't want anyone searching my site with apostrophes in their keywords and seeing what amounts to imcompetence rolling down on their screen in the form of errors.

 

Apostrophes in search is nothing to worry about for most, but...I sell actors. And many of those people have apostrophes in their names. Should be simple enough to fix. I just don't know how.

 

I messed around with your code suggestions in advanced_search_result.php all evening and nothing is working. Am I in the right file?

 

Where does the assumption come from that I am the mechanic here?

I am just a store owner as yourself and 2 years ago I also did not know diddly about osc/php/mysql.

I just have a little more experience than you.

But I would not have that experience if I had adopted the mindset "I don't want to know how it works so don't explain it to me, just give me the code and tell me where to put it."

 

To continue your car analogy, if you go to a car mechanic and say "I have a car and I want to double the horsepower. But don't explain to me how it works and why as I am not a mechanic, just tell me which cables to unplug and where to find them."

I think you would also receive some bewildered looks.

 

"I can appreciate what's happening here, I really can. You're the one who knows everything and you find my questions tedious and silly."

I never said or thought your questions are tedious and silly so these presumptions say a little bit more about your mindset than they do about mine.

 

"I cannot read the code, Boxtel. I'm not a programmer. All I can do is follow instructions"

These are your words not mine, all I stated about that is that if you have no intention to change that then you will find managing an osc store increasingly difficult and problematic as after a limited time no osc store is stock anymore and few alterations will be successful with just following instructions.

 

I now that that is not the answer you wanted but that does not make it any less valid.

Treasurer MFC

Link to comment
Share on other sites

Alright. No more arguing. I finally figured it out.

 

I took that line of code you offered...$keywords = str_replace("'","\'",$keywords);

 

And I added it to your first function in post #1.

 

No wonder I was so upset last night. I'm hanging out messing around with advanced_search_result.php for 2 days and all that time....I was simply in the wrong place. ??? But, hey...I didn't know any better and that's all I can say.

 

Your first function now looks like this in my website:

 

function highlight_viewable_text ($text, $keywords, $class) {
if (($text == '') or ($keywords == '')) return $text;
$keywords = str_replace("'","\'",$keywords);
if (!($check = tep_parse_search_string($keywords, $search_keywords))) return $text;
$strip_array = array('and', 'or', 'AND', 'OR');
$result = $text;
for ($i = 0; $i<sizeof($search_keywords); $i++) {
if (!in_array($search_keywords[$i], $strip_array)) {
$result = highlight_text($result, $search_keywords[$i], $class);
}
}
return $result;
}

 

Searches are now a thing of beauty when using apostrophes. :)

 

Thank You for all of your help, Boxtel and I wish you a Merry Christmas. :D

Link to comment
Share on other sites

Alright. No more arguing. I finally figured it out.

 

I took that line of code you offered...$keywords = str_replace("'","\'",$keywords);

 

And I added it to your first function in post #1.

 

No wonder I was so upset last night. I'm hanging out messing around with advanced_search_result.php for 2 days and all that time....I was simply in the wrong place. ??? But, hey...I didn't know any better and that's all I can say.

 

Your first function now looks like this in my website:

 

function highlight_viewable_text ($text, $keywords, $class) {
if (($text == '') or ($keywords == '')) return $text;
$keywords = str_replace("'","\'",$keywords);
if (!($check = tep_parse_search_string($keywords, $search_keywords))) return $text;
$strip_array = array('and', 'or', 'AND', 'OR');
$result = $text;
for ($i = 0; $i<sizeof($search_keywords); $i++) {
if (!in_array($search_keywords[$i], $strip_array)) {
$result = highlight_text($result, $search_keywords[$i], $class);
}
}
return $result;
}

 

Searches are now a thing of beauty when using apostrophes. :)

 

Thank You for all of your help, Boxtel and I wish you a Merry Christmas. :D

 

very well done and now you can handle any other strange character that comes along with all those foreign actors.

Marry Christmas to you as well.

Treasurer MFC

Link to comment
Share on other sites

Alright. No more arguing. I finally figured it out.

 

Searches are now a thing of beauty when using apostrophes. :)

 

Thank You for all of your help, Boxtel and I wish you a Merry Christmas. :D

 

 

Puggybelle, I have followed this thread for more than a week now, and I'm glad that you and boxtel came to terms, so to speak. But since you had a very hard time to figure out how to accomplish this, why not sum this thread up by posting the step by step to get the Highlight search working!? I believe you would help a lot of php-newbies by doing that, so what do you say? After all, you have done a lot of thinking here, I think.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...