Apostrophes producing strange results with similar_text

I have a webpage that allows editors to make changes to the title of a testimonial. Without fail, whenever there is an apostrophe in the title, similar_text thinks that the text has been changed when $originalTitle and $modifiedTitle are compared using similar_text.

If I remove the apostrophe and do the test again, not making any text changes to the form and then click the submit button, similar_text rightfully concludes no changes were made between $originialTitle and $modifiedTitle. Why is this happening when my code below actually removes any apostrophes before comparing with similar_text?

Thanks!

$originalTitle	= str_replace("'", "", $originalTitle);

$modifiedTitle	= str_replace("'", "", $modifiedTitle);

$titleLength	= similar_text($modifiedTitle, $originalTitle, $percentChange);

Are you saying that your function similartext() gives results saying that the title has changed even tho it is not changed when the title had apostrophe in it? Sounds like the problem could be inside similartext() function then.

Maybe provide a test case scenario with input and results? Kind of hard to figure out what is the problem from your post. And I am not probably only one since this topic has not been answered yet. Also providing similar_text() code could help.

Have you done any debugging to find out if the titles are/are not different when passing them to the similar_text()?

similar_text() is a standard PHP function, not the OPs code.

But yes, what debugging have you done to see that you’re correctly removing the apostrophes from the data prior to checking the similarity? Can you show the code where those two variables originate, i.e. do you do any work on the form post variables and the data retrieved from the database for comparison prior to this point, such as escaping the strings? It seems strange that you’re stripping them out, yet still getting an issue. But if you just want to check whether the title has changed, aren’t you over-complicating things by using similar_text()?

After praying and asking the Lord for some direction on this mind numbing bug, I found a forum on another website that mentioned the exact problem I was having. Apparently I needed to use a double quote, instead of a single one, in my form input. See the code below and how the double quote is escaped. This fixed everything. Praise the Lord!

$originalTestimonial = htmlspecialchars($originalTestimonial);    
$pageContent .= "<input class=form name='originalTestimonial' id='originalTestimonial' type='hidden' value=\"$originalTestimonial\">";

Ah, no wonder no-one spotted it. Personally I’d be consistent though - if you’re going to use double quotes around the value attribute, use them around all the others as well.

$pageContent .= '<input class="form" name="originalTestimonial" id="originalTestimonial" type="hidden" value="' . $originalTestimonial. '">';

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.