<?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>umlaut | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/umlaut/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Mon, 18 Jul 2022 10:03:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Converting umlaute with symfony String component</title>
		<link>https://nerdpress.org/2022/07/18/converting-umlaute-with-symfony-string-component/</link>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Mon, 18 Jul 2022 10:03:11 +0000</pubDate>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[umlaut]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3159</guid>

					<description><![CDATA[<p>There are multiple PHP native ways to convert umlaute and other special characters to ascii save formats, but most of them i experience insufficient in the one or other way. Since i am using symfony anyway i can use the String component which has a handy slugger which converts any characters into safe ascii characters.It &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2022/07/18/converting-umlaute-with-symfony-string-component/" class="more-link">Continue reading<span class="screen-reader-text"> "Converting umlaute with symfony String component"</span></a></p>
The post <a href="https://nerdpress.org/2022/07/18/converting-umlaute-with-symfony-string-component/">Converting umlaute with symfony String component</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>There are multiple PHP native ways to convert umlaute and other special characters to ascii save formats, but most of them i experience insufficient in the one or other way.</p>



<p>Since i am using symfony anyway i can use the <a href="https://symfony.com/doc/current/components/string.html" target="_blank" rel="noreferrer noopener" title="https://symfony.com/doc/current/components/string.html">String component</a> which has a handy slugger which converts any characters into safe ascii characters.<br />It is very flexible and can be customized with locales, custom replacements and closures.</p>


<pre class="wp-block-code"><span><code class="hljs language-php">$umlautString = <span class="hljs-string">"Müller Meier"</span>;
$slugger = <span class="hljs-keyword">new</span> Symfony\Component\String\Slugger\AsciiSlugger(<span class="hljs-string">'de'</span>);
$slugger-&gt;slug($umlautString, $seperator = <span class="hljs-string">' '</span>)-&gt;toString();
<span class="hljs-keyword">echo</span> $umlautString; <span class="hljs-comment">// mueller meier</span></code></span></pre>


<p>I guess this will become my go-to method resp. slugger to convert umlaute in any PHP application.</p>



<span id="more-3159"></span>



<p>One example why the PHP native ways have their downsides is iconv.<br />Iconv can transliterate umlauts:</p>


<pre class="wp-block-code"><span><code class="hljs language-php">$string = iconv(<span class="hljs-string">'UTF-8'</span>, <span class="hljs-string">'ASCII//TRANSLIT'</span>, $string);</code></span></pre>


<p>But it can not handle the locale in a convenient way but relies on the system wide locale instead.<br />You need to use the global method <code>setlocale</code> before using it and reset the locale after usage again, which can cause sideeffects and is not very convenient.</p>



<p>From: <a href="https://www.php.net/manual/de/function.iconv.php#105507">https://www.php.net/manual/de/function.iconv.php#105507</a></p>


<pre class="wp-block-code"><span><code class="hljs language-php">$utf8_sentence = <span class="hljs-string">'Weiß, Göbel'</span>;
<span class="hljs-comment">//transliterate</span>
$trans_sentence = iconv(<span class="hljs-string">'UTF-8'</span>, <span class="hljs-string">'ASCII//TRANSLIT'</span>, $utf8_sentence);
<span class="hljs-comment">//gives &#91;Weiss, Gobel]</span>
<span class="hljs-comment">//Germany</span>
setlocale(LC_ALL, <span class="hljs-string">'de_DE'</span>);
$trans_sentence = iconv(<span class="hljs-string">'UTF-8'</span>, <span class="hljs-string">'ASCII//TRANSLIT'</span>, $utf8_sentence);
<span class="hljs-comment">//gives &#91;Weiss, Goebel]</span></code></span></pre>


<p>Thanks to this StackOverflow answer for the tip with the symfony String component. <a href="https://stackoverflow.com/a/68742023/541949" target="_blank" rel="noreferrer noopener">https://stackoverflow.com/a/68742023/541949</a></p>The post <a href="https://nerdpress.org/2022/07/18/converting-umlaute-with-symfony-string-component/">Converting umlaute with symfony String component</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
