简体   繁体   English

在 GET 表单提交中解释瑞典语字母(å、ä、ö)

[英]Interpreting Swedish letters (å, ä, ö) in a GET form submission

I am currently creating a custom WordPress theme.我目前正在创建一个自定义 WordPress 主题。 A couple of the archive pages will include hidden forms (represented by pictures).一些存档页面将包含隐藏的 forms(以图片表示)。 Each of these forms represent a post.这些 forms 中的每一个都代表一个帖子。 When a user clicks on a form, a GET request is run and the browser retrieves the content of the specific post.当用户单击表单时,将运行 GET 请求并且浏览器检索特定帖子的内容。

Each hidden form takes the title of the post as the value (as a query string like this: /lokaler/?lokal=Studion ('Studion' being the title, 'lokaler' being the post type)).每个隐藏表单都将帖子的标题作为值(作为这样的查询字符串:/lokaler/?lokal=Studion('Studion' 是标题,'lokaler' 是帖子类型))。 This works fine and I get the information needed, no problem.这工作正常,我得到所需的信息,没问题。

The problem arises when I want to do the same with a title that contains Ä.当我想对包含 Ä 的标题执行相同操作时,就会出现问题。 The query /lokaler/?lokal=Sammanträdesrummet (obviously) doesn't work, but I can't seem to find how to work around it... I am using a combination of JavaScript and PHP on this page.查询 /lokaler/?lokal=Sammanträdesrummet (显然)不起作用,但我似乎找不到解决方法......我在此页面上使用 JavaScript 和 PHP 的组合。

How would I be able to work around it so that the GET request works but I don't have to change the title of the post?我将如何解决它以使 GET 请求有效但我不必更改帖子的标题?

//how the form is created:

 <form method="GET" id="lokal_<?php echo get_the_title(); ?>" class="single_lokal_container single_lokal_lokal">
   <input type="hidden" name="lokal" value="<?php echo get_the_title(); ?>">

   etc...


//how post is queried
$current_lokal_from_uri = htmlspecialchars($_GET['lokal']);

            $args3 = array(
                'name'          =>      $current_lokal_from_uri,
                'post_type'     =>      'lokaler'
            );

You should be able to pass these through GET parameters fine.您应该能够通过 GET 参数很好地传递这些。 Just be sure to encode and decode consistently:只要确保一致地编码和解码:

var plain = 'åäö';
var urlencoded = encodeURIComponent(plain);
var unencoded = decodeURIComponent(urlencoded);

console.log('plain', plain);// plain åäö 
console.log('urlencoded', urlencoded);// urlencoded %C3%A5%C3%A4%C3%B6 
console.log('unencoded', unencoded);// unencoded åäö
echo urldecode('%C3%A5%C3%A4%C3%B6'); // åäö

Bear in mind that HTMLSpecialChars is for encoding characters to html-safe flavours not parameter encoding (eg & becomes &amp; and < becomes $lt; )请记住,HTMLSpecialChars 用于将字符编码为 html 安全风格而不是参数编码(例如&变为&amp;并且<变为$lt;

I have managed to solve the problem.我已经设法解决了这个问题。

I used urlencode and decode for the value of the form.我使用 urlencode 和 decode 作为表单的值。 The next problem arose when trying to make a wp_query from the decoded value.尝试从解码值生成 wp_query 时出现下一个问题。 At the end, it was a simple fix: i had to replace the ä from the decoded value with an a:最后,这是一个简单的修复:我必须将解码值中的 ä 替换为 a:


            $current_lokal_from_uri_decoded = urldecode($current_lokal_from_uri);      

            if(strpos($current_lokal_from_uri_decoded, 'ä') !== false) {
                $current_lokal_from_uri_decoded = str_replace('ä', 'a', $current_lokal_from_uri_decoded);
            } 

            $args3 = array(
                'name'          =>      $current_lokal_from_uri_decoded,
                'post_type'     =>      'lokaler'
            );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM