Welcome Guest, Not a member yet? Register   Sign In
weird characters in the url
#1

[eluser]dianikol85[/eluser]
here is the situation

i have a search form and when i submit it i add the variables in the url in order to have the pagination according to the search results. So far so good.

Because i am from Greece i need to pass greek characters, see an example

Code:
http://df.loc/admin/user/index/u_id/asc/search_by_name=τεστ

The value after the search_by_name keyword is greek.

but when i echo the url it prints

Code:
http://df.loc/admin/user/index/u_id/asc/search_by_name=CF84CEB5CF83CF84

so i can't execute successfully the sql query in my model because of these weird chars.

My collation are utf-8 and the charset utf8 also


Any suggestions to produce 'τεστ'
#2

[eluser]spider pig[/eluser]
Since the search form user could enter any character, even characters that are not allowed in the URL. I would suggest that you put the search term in a session variable. For example:

Code:
$this->session->userdata('searchtext', $this->input->post('searchtext'));
#3

[eluser]shadow player[/eluser]
"CF84CEB5CF83CF84" is your string in hexadecimal representation.

Code:
function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

echo hexToStr('CF84CEB5CF83CF84');

That will output 'τεστ' Smile
#4

[eluser]osci[/eluser]
I'm from Greece too and I also send in url greek words. in ie for my news i have
/news/1/Νέο-1

in application/config/config.php i have
Code:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'.
   'αάβγδεέξηήθιίϊκλμνξοόπρσςτυύϋφχψωώ'.
   'ΑΆΒΓΔΕΈΖΗΉΘΙΊΪΚΛΜΝΞΟΌΠΡΣΤΥΎΫΦΧΨΩΏ';

and to produce them I have in a helper
Code:
function string_for_uri($str)
{
    $str = str_replace(array('"', "'","!","?","@","#","$","%","^","*",";","`","~",), '', $str);
    $str = str_replace(array("/", ",","."," ","<",">","(",")","[","]","{","}","&","-","="), '-', $str);
    return $str;
}
removing unwanted characters.

Edit:
This way I can have urls containing greek like
http://www.netid.gr/news/page/1/Στατικές...ες-Όχι-πια
#5

[eluser]dianikol85[/eluser]
den perimena na synantiso ellina. geia sou file


Can you give me an example how to pass it to the model?

When i retreive the segment from the url it returns hex
#6

[eluser]dianikol85[/eluser]
[quote author="shadow player" date="1302451507"]"CF84CEB5CF83CF84" is your string in hexadecimal representation.

Code:
function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

echo hexToStr('CF84CEB5CF83CF84');

That will output 'τεστ' Smile[/quote]


when i use hexToStr with the actual value like this
Code:
hexToStr('CF84CEB5CF83CF84');
it works but when i pass in a parameter
Code:
hexToStr($hex_to_string)
it produces trush
#7

[eluser]osci[/eluser]
I've noticed there are others too Smile

for a url like /news/page/Τιμή

In my controller
Code:
$q=$this->uri->segment(3);
   $test= $this->news_model->get_record_slug($q)->row();

and in my model
Code:
function get_record_slug($slug)
{
    $query = $this->db->from('t_news')->like('title', $slug)->get();
    return $query;
}

This matches first record only, but tested it in a hurry in my lab site, so I just copied specific functions to reproduce your segment, but you should see the point. No need to do anything with hex Smile
But everything should be configured for utf8 (db, db connection,page charset, etc)
I think that's the only requirement. Also you have to permit greek chars as in above comment. (mind that I use core and not reactor in my site lab, but since I'm also developing and testing on reactor, and having looked at the changes I don't think there's any issue concerning this difference)
#8

[eluser]dianikol85[/eluser]
i got your point osci.

i may confuse you with my previews posts.

I will explain my technique for search form and pagination. I'll keep it short

i have a controller and a method index in it. In the index i fetch a db table and pass it to a view with pagination also

Code:
class Welcome extends CI_Controller {
    
   function index($search_terms = '')
   {

        //  retrieve data
        //  configure the pagination
        //  load the view
   }
}

In the view i have also a search form that when i submit it , it goes to a pre-search method tha produces the query string, When the query is ready i redirect to the index and pass to it the query string . Please notice in the code above i have
Code:
function index($search_terms = '')

The var $search_terms i pass it to the model where i can have $this->db->like('name',$search_terms);

Code:
function pre_search()
   {
        
        //  retrieve submited data from $_POST
        //  generate the query string in this format: $keyword = search_by_name=Doe+John
        //  and pass it to the view
        redirect('index/'.$keyword);
   }


The procedure above has as a result to produce a url like that
Code:
http://test.loc/welcome/index/search_by_name=Doe+John

and that's how i accomplish search results with pagination.

The problem is when i search with a greek keyword
Code:
search_by_name=τεστ

the $search_terms var contains hex value like that

Code:
search_by_name = CF84CEB5CF83CF84

so the sql query fails to return any value.


I hope you understand what i wrote , my english aren't so good.

Do you suggest a different method to achive search results with pagination?
#9

[eluser]shadow player[/eluser]
Quote:I hope you understand what i wrote , my english aren’t so good.
It's ok, i understood it.
Quote:Do you suggest a different method to achive search results with pagination?
Personally I would write the pagination myself, it's not that hard and you can achieve much more flexibility.

What (source code) do you get when you do this:
Code:
echo $search_terms;
In your Index method when receiving $_POST information.

My guess would be that it's not being transferred as UTF-8 but try it and let me know, this can't be that difficult to solve.
#10

[eluser]dianikol85[/eluser]
i get the $_POST in the pre_search method where i produce a string and then passing it as parameter

with this


redirect('index/'.$search_terms).

With that way i can have a big search form but only one segment in the url.

So if i echo out the $search_terms it shows somthing like that

search_by_name= CF 84 CE B5 CF 83 CF 84


i add spaces because without them it will echo the greek word in the post

which is the greek word in hex




Theme © iAndrew 2016 - Forum software by © MyBB