Page 1 of 1

Trouble working with string functions with latin characters

Posted: Thu Feb 10, 2011 12:04 pm
by fcnunez
I'm working with latin characters (Spanish) and I want to compare strings using strtok and strcmp to find some key words, the problem is when I want to search words like "Baño" , "Lavandería" into the string that I input in the form, the thing is that if I use strcmp in the action form (in this case import.php) like :
Previously I inserted in the form in <textarea> "Baño"
So:

Code: Select all

if(strcmp("Baño",$_POST['txtDescription'])==0)
{echo "The word was found"}
else
{echo "The word wasn't found"}
The expected result is that "The word was found" but I'm not sure why it doesn't happens
In the other hand
if I try like this

Code: Select all

if(strcmp("Baño","Baño")==0)
{echo "The word was found"}
else
{echo "The word wasn't found"}
Of course The word was found


I'd like to solve this , I've tried different aproaches

Like:

Code: Select all

$txt=$_POST['txtDescription'];
$find=array("á","é","í","ó","ú","ñ");
$replace=array("a","e","i","o","u","n");
$txt2=str_replace($find,$replace,$txt);
echo $txt2."<br>";
I did this because I thought that I could be easier just compare Banos=Banos,
but when I execute the code
// $_POST['txtDescription']=Baño
the result of echo is
Baño , but I expected Bano.

So I think that the 'problem' comes from $_POST['txtDescription'] because if I insert the string directly in strcmp, or str_replace it works, but when comes from $_POST['txtDescription'] doesn't.

this is the way that I insert the string

Code: Select all

<form action="import.php" method="post"  enctype="text/plain">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="anchoLabel" valign="top">Texto:</td>
<td colspan="3"><label>
<textarea name="txtDescripcion" id="txtDescription" cols="45" rows="5" class="texto1"></textarea>
</label></td>
</tr>
</table>
<td width="40%" valign="middle">
<label><input type="image"
name="btnEnviar" id="btnEnviar" value="Subir"
src="images/btn_subir.png" /><!--<a onclick="mandarFormulario();" style="cursor:pointer;"><img src="images/btn_subir.png"  border="0" align="absmiddle" /></a>-->
&nbsp;</label>
</td>
</form>


Thanks in advance for the support

Re: Trouble working with string functions with latin charact

Posted: Thu Feb 10, 2011 5:42 pm
by visual77
That sounds like a character encoding issue. If one is in UTF-8 and the other is in ISO-8859-1, the string comparison won't work properly.

Try using iconv and mb_detect_encoding to convert from ISO-8859-1 to UTF-8 (or vice versa) so that you know both strings are in the same character set.

This page may be helpful : http://visual77.com/blog/2010/02/dealin ... -encoding/

Re: Trouble working with string functions with latin charact

Posted: Sun Feb 13, 2011 4:34 pm
by fcnunez
Thanks, I follow the steps like your url, and it works!

So to finish the trouble I just have to find the latin characters in UTF-8 and do the strcmp

Thanks ;)