Codeigniter 3 应用程序错误:如果标题中有变音符号,则无法将标题转换为 slug
Codeigniter 3 application bug: converting title to slug does not work if there are diacritics in the title
我正在使用 Codeigniter 3.1.8 和 Bootstrap 4.[=15= 开发一个基本的博客应用程序]
单曲 post URL 由 post 的标题和 url_title($this->input->post('title'), 'dash', TRUE)
组成。整个post创建函数是这样的:
public function create() {
// Only logged in users can create posts
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$data = $this->get_data();
$data['tagline'] = "Add New Post";
if ($data['categories']) {
foreach ($data['categories'] as &$category) {
$category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
}
}
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('desc', 'Short description', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if($this->form_validation->run() === FALSE){
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post');
$this->load->view('partials/footer');
} else {
// Create slug (from title)
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, null);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'default.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Posts_model->create_post($post_image, $slug);
$this->session->set_flashdata('post_created', 'Your post has been created');
redirect('/');
}
}
我确信它运行良好,直到我在标题中添加了一个带有变音符号的 post。 "La mulți ani România!" 导致 slug la-mul??i-ani-rom??nia 而不是 la-multi-ani-romania。
我试图用 Codeigniter 的 convert_accented_characters()
解决这些问题:
$slug = convert_accented_characters(url_title($this->input->post('title'), 'dash', TRUE));
即使我确实加载了测试助手,它也不工作:
$autoload['helper'] = array('url', 'form', 'date', 'text');
我还有哪些其他选择?我该如何解决这个问题?
我使用 url_title()
和 convert_accented_characters()
的顺序是错误的。
这是正确的一个:
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
现在可以正常使用了。
我正在使用 Codeigniter 3.1.8 和 Bootstrap 4.[=15= 开发一个基本的博客应用程序]
单曲 post URL 由 post 的标题和 url_title($this->input->post('title'), 'dash', TRUE)
组成。整个post创建函数是这样的:
public function create() {
// Only logged in users can create posts
if (!$this->session->userdata('is_logged_in')) {
redirect('login');
}
$data = $this->get_data();
$data['tagline'] = "Add New Post";
if ($data['categories']) {
foreach ($data['categories'] as &$category) {
$category->posts_count = $this->Posts_model->count_posts_in_category($category->id);
}
}
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('desc', 'Short description', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
if($this->form_validation->run() === FALSE){
$this->load->view('partials/header', $data);
$this->load->view('dashboard/create-post');
$this->load->view('partials/footer');
} else {
// Create slug (from title)
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$slugcount = $this->Posts_model->slug_count($slug, null);
if ($slugcount > 0) {
$slug = $slug."-".$slugcount;
}
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'default.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->Posts_model->create_post($post_image, $slug);
$this->session->set_flashdata('post_created', 'Your post has been created');
redirect('/');
}
}
我确信它运行良好,直到我在标题中添加了一个带有变音符号的 post。 "La mulți ani România!" 导致 slug la-mul??i-ani-rom??nia 而不是 la-multi-ani-romania。
我试图用 Codeigniter 的 convert_accented_characters()
解决这些问题:
$slug = convert_accented_characters(url_title($this->input->post('title'), 'dash', TRUE));
即使我确实加载了测试助手,它也不工作:
$autoload['helper'] = array('url', 'form', 'date', 'text');
我还有哪些其他选择?我该如何解决这个问题?
我使用 url_title()
和 convert_accented_characters()
的顺序是错误的。
这是正确的一个:
$slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
现在可以正常使用了。