<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Naeh.net &#187; conversion</title>
	<atom:link href="http://naeh.net/tag/conversion/feed/" rel="self" type="application/rss+xml" />
	<link>http://naeh.net</link>
	<description>Le mémo du développeur</description>
	<lastBuildDate>Wed, 30 Jun 2010 18:56:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>convertir un array en utf8</title>
		<link>http://naeh.net/convertir-un-array-en-utf8/</link>
		<comments>http://naeh.net/convertir-un-array-en-utf8/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 20:03:22 +0000</pubDate>
		<dc:creator>Naeh</dc:creator>
				<category><![CDATA[PHP / MySQL]]></category>
		<category><![CDATA[Programmation]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://naeh.net/?p=100</guid>
		<description><![CDATA[dans un précédent article nous avons vu comment convertir un texte en utf8.
aujourd'hui on va voir comment faire la même chose mais avec un tableau, sans passer par un foreach, ni aucune autre boucle.
pour cela nous allons utiliser une seule fonction qui convertira ce qu'on lui donne en entrée en utf8.
pour l'instant elle va savoir [...]]]></description>
			<content:encoded><![CDATA[<p>dans un <a href="http://naeh.net/comment-convertir-un-texte-en-utf-8/">précédent article</a> nous avons vu comment convertir un texte en utf8.</p>
<p>aujourd'hui on va voir comment faire la même chose mais avec un tableau, sans passer par un foreach, ni aucune autre boucle.</p>
<p>pour cela nous allons utiliser une seule fonction qui convertira ce qu'on lui donne en entrée en utf8.<br />
pour l'instant elle va savoir gérer, les chaînes de caractères et les tableaux.</p>

<div class="wp_codebox"><table><tr id="p1003"><td class="code" id="p100code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> toUTF8<span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/is_array"><span style="color: #990000;">is_array</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <a href="http://www.php.net/array_walk_recursive"><span style="color: #990000;">array_walk_recursive</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #339933;">,</span> <a href="http://www.php.net/create_function"><span style="color: #990000;">create_function</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;$item, $index'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'$item = toUTF8($item);'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$param</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <a href="http://www.php.net/mb_detect_order"><span style="color: #990000;">mb_detect_order</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UTF-8, ISO-8859-15, ISO-8859-1, Windows-1252'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">//parfois si le dernier caractère de la chaine est accentué, la conversion peut foirer, </span>
    <span style="color: #666666; font-style: italic;">//donc on force avec un caractère qui ne l'est pas.</span>
    <span style="color: #666666; font-style: italic;">//(astuce trouvée sur les commentaires de la doc sur php.net)</span>
    <span style="color: #000088;">$param</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'_'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$currentCharset</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mb_detect_encoding"><span style="color: #990000;">mb_detect_encoding</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$currentCharset</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$param</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mb_convert_encoding"><span style="color: #990000;">mb_convert_encoding</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$currentCharset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <a href="http://www.php.net/substr"><span style="color: #990000;">substr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>et voilà, cette fonction convertira array ou string en utf8 sans brancher...</p>
<p>en bonus voici une fonction somme que j'ai trouvé dans les commentaire de la doc sur php.net (une sorte de coup de coeur pour moi :-D)</p>

<div class="wp_codebox"><table><tr id="p1004"><td class="code" id="p100code4"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$s</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/func_get_args"><span style="color: #990000;">func_get_args</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$s</span><span style="color: #339933;">+=</span> <a href="http://www.php.net/is_numeric"><span style="color: #990000;">is_numeric</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span>?<span style="color: #000088;">$a</span><span style="color: #339933;">:</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span> sum<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return 21</span>
<span style="color: #b1b100;">print</span> sum<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return 6</span>
<span style="color: #b1b100;">print</span> sum<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// will return 10</span></pre></td></tr></table></div>

<p>trop fort non ?</p>
]]></content:encoded>
			<wfw:commentRss>http://naeh.net/convertir-un-array-en-utf8/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Comment convertir un texte en UTF-8</title>
		<link>http://naeh.net/comment-convertir-un-texte-en-utf-8/</link>
		<comments>http://naeh.net/comment-convertir-un-texte-en-utf-8/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 14:04:27 +0000</pubDate>
		<dc:creator>Naeh</dc:creator>
				<category><![CDATA[PHP / MySQL]]></category>
		<category><![CDATA[Programmation]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[decode]]></category>
		<category><![CDATA[encodage]]></category>
		<category><![CDATA[encode]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[utf-8]]></category>

		<guid isPermaLink="false">http://naeh.net/comment-convertir-un-texte-en-utf-8/</guid>
		<description><![CDATA[voici une fonction simple mais efficace pour convertir un texte en UTF-8 sans se préoccuper de sa provenance (encodage d'origine).

setlocale&#40;LC_ALL, 'fr_FR.utf8'&#41;; //selon la configuration du serveur cela peut être : fr_FR
&#160;
mb_detect_order&#40;array&#40;'UTF-8', 'ISO-8859-1', 'ISO-8859-15', 'Windows-1252'&#41;&#41;; //l'extention mb_string est installée par defaut sur php5
function toUTF8&#40;$string&#41;&#123;
&#160;
   $from = mb_detect_encoding&#40;$string&#41;;
&#160;
if &#40;$from != 'UTF-8'&#41; &#123;
&#160;
    [...]]]></description>
			<content:encoded><![CDATA[<p>voici une fonction simple mais efficace pour convertir un texte en UTF-8 sans se préoccuper de sa provenance (encodage d'origine).</p>

<div class="wp_codebox"><table><tr id="p436"><td class="code" id="p43code6"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/setlocale"><span style="color: #990000;">setlocale</span></a><span style="color: #009900;">&#40;</span>LC_ALL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'fr_FR.utf8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//selon la configuration du serveur cela peut être : fr_FR</span>
&nbsp;
<a href="http://www.php.net/mb_detect_order"><span style="color: #990000;">mb_detect_order</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'UTF-8'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ISO-8859-1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ISO-8859-15'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Windows-1252'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//l'extention mb_string est installée par defaut sur php5</span>
<span style="color: #000000; font-weight: bold;">function</span> toUTF8<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #000088;">$from</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mb_detect_encoding"><span style="color: #990000;">mb_detect_encoding</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$from</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mb_convert_encoding"><span style="color: #990000;">mb_convert_encoding</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UTF-8'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$from</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://naeh.net/comment-convertir-un-texte-en-utf-8/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
