How to compare string to accents in php

Asked

Viewed 735 times

0

I have a problem that I could not solve in php, I have a field varchar in the MySQL with accentuation, I rescue the same and use the htmlentities for the string to have the correct accent. I need to compare the variable coming from the bank and mark a radio field if the condition is true, but I’m not getting what I have and this:

Rescue of the variable:

$Motivo = htmlentities($row_DocContratacao['MotivoContratacao']);

Value comparison

<input type="radio" name="Motivo" id="motivo-4" value="Recontratação"<?php if ($Motivo=="Recontratação") echo ' checked="checked"'; ?> /> 

The amount redeemed is Recontratação but the radio field is not marked.

  • You can also remove the accents and make the comparison without accents.

  • Hello @Earendul, but I don’t understand why I couldn’t make the comparison with accent.

  • 1

    the condition does not match because when applying htmlentities the comparison gets 'Recontrata&ccedil;&atilde;o' == 'Recontratação', remove the call from htmlentities should solve the problem

  • Hello @Sanction, in my case, with htmlentities I can rescue the variable with the correct accent, but I can’t compare it.

1 answer

1


When using htmlentities you will be changing the String to "Recontrata & ccedil;& atilde;o" which when compared to "Recontreatç& atilde;o" will return you a false boolen.

Try to convert inside your IF to get the expected result, below is an example of how to do.

<input type="radio" name="Motivo" id="motivo-4" value="Recontratação"<?php if ($Motivo==htmlentities("Recontratação")) echo ' checked="checked"'; ?> /> 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.