Reverse htmlspecialchars or something

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Reverse htmlspecialchars or something

Post by Punkis »

I have a project that gets from mysql the data that i search.
Until 10 minutes ago, the results could not display me correctly the Greek characters from mysql.
The search system searches the title of the item which contains English and Greek characters.

I solve this problem with this :
I use notepad for any encoding i do between ansi, unicode and utf8.
I wrote into notepad the title in English and for the Greek letters i wrote them in HTML encoded characters and i saved it as ANSI.

On phpmyadmin I uploaded the file choosing utf8 for Character set of the file and ANSI for sql compatibility.

i searched for that English word i wrote in the title and displayed to me the result WITH GREEK LETTERS correctly.
This is the only way to display Greek letters. I tried everything.

The problem is :
When i searched using that Greek letters that i wrote in the title the result showed me nothing. Then i searched again using the HTML encoded characters i used for that title and i got the result with Greek letters!

Having in mind that with HTML encoded characters the system found me the result, I guess there must be a line that it will convert the Greek characters to HTML encoded characters before it gives me the results.

Anyone knows anything about this?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reverse htmlspecialchars or something

Post by josh »

Code: Select all

mysql_query("SET NAMES 'utf8'");    
Try calling that before you call your query?
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

thank you for your reply.

Where i write that?
:banghead:
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

<?php
$str = '<p>this -> " & & </p>';

echo htmlentities($str);


?>


this should make a work right? but why it didnt?!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reverse htmlspecialchars or something

Post by josh »

When i searched using that Greek letters that i wrote in the title the result showed me nothing. Then i searched again using the HTML encoded characters i used for that title and i got the result with Greek letters!
are you trying to search the database for a greek string, or output a greek string?
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

My greek data is written in the database using the decimal format http://htmlhelp.com/reference/html40/en ... mbols.html

The only way to get the results displayed is when i search with those decimal codes.

I want to convert the greek letters that i write in the searchbox to those decimal codes before it gives me the results, with this way i will get the correct search results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reverse htmlspecialchars or something

Post by josh »

You can make a "data dictionary" by defining an array with strings as keys, you can write a 3 line function to encode and decode based on the array

Code: Select all

 
 
function lookupSymbol( $symbol)
{
$lookup = array( 'amperstand' => '&' );
return isset($lookup[$symbol])?$lookup:$symbol;
}
echo lookupSymbol( 'amperstand' ); // outputs '&'
echo lookupSymbol( 'doesnt exist' ); // outputs 'doesnt exist'
 
Last edited by josh on Sat Sep 20, 2008 8:30 pm, edited 1 time in total.
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

hm.. you sound correct.
but i do not know how do it, but i will if you write me an example. thanks!
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reverse htmlspecialchars or something

Post by josh »

edited my post before you replied.
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

thank you a lot!
i hope that will work.

can I ask you something else?
This is the part of search.php that does the work ( i guess, i am 99% sure )
My question is, where i write the function?
thanks

Code: Select all

<?php #//v.3.3.0
 
 
require('./includes/config.inc.php');
$NOW = date("YmdHis",mktime(date("H")+$SETTINGS['timecorrection'],date("i"),date("s"),date("m"), date("d"),date("Y")));
 
 
if (!ini_get('register_globals')) {
   $superglobales = array($_POST, $_GET);
   foreach ($superglobales as $superglobal) {
       extract($superglobal, EXTR_SKIP);
   }
}
 
$q = trim($_GET['q']);
 
 
 
$query = "".$q;
$query = "".$query; // set $query variable if it's not set yet
$searchQuery = $query;
$qquery = ereg_replace("%","\\%",$query);
$qquery = ereg_replace("_","\\_",$qquery);
 
if ( strlen($query)==0 ) {
    include "header.php";
    include phpa_include("template_empty_search.html");
    include "footer.php";
    exit;
}
 
/* generate query syntax for searching in auction */
$search_words = explode (" ", $qquery);
 
/* query part 1 */
$qp1 = "";
$qp = "";
$qp1 = htmlentities($qp1);
$qp1 .=
" (title LIKE '%".
addslashes($qquery).
"%' OR id=".intval($q).")  ";
 
$qp .= " (cat_name LIKE '%".addslashes($qquery)."%') ";
 
$addOR = true;
while ( list(,$val) = each($search_words) ) {
    $val = ereg_replace("%","\\%",$val);
    $val = ereg_replace("_","\\_",$val);
    if ($addOR) {
        $qp1 .= " AND ";
        $qp .= " AND ";
    }
    $addOR = true;
    
    $qp1 .=
    " (title LIKE '%".
    addslashes($val).
    "%')  ";
    
    $qp .= "(cat_name LIKE '%".addslashes($qquery)."%') ";
}
//  die($qp1);
//  print $qp."<BR>";
 
$sql_count = "SELECT count(*) FROM PHPAUCTIONXL_auctions 
                WHERE ( $qp1 ) 
                AND ( closed='0')  
                AND ( suspended='0') 
                AND private='n' 
                AND starts<=".$NOW." 
                ORDER BY buy_now";
$sql = "SELECT * FROM PHPAUCTIONXL_auctions 
            WHERE ( $qp1 ) 
            AND ( closed='0')  
            AND ( suspended ='0') 
            AND private='n' 
            AND starts<=".$NOW." 
            ORDER BY buy_now";
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reverse htmlspecialchars or something

Post by josh »

You might need to use str_replace or preg_replace then
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

oh! i did a small test and it worked!
But with English letters. I will test in with a Greek letter that it will be replaced by the decimal html code.
The problem is that when i write something in greek and save the file, it turns to decimal by its self!
I will test it now. Hope it works.
Please guide me if you have something to add.
Millions of thank you
User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

It didn't work.
$qquery = ereg_replace("ι","&#953 ;",$query);

may because the page is saved as us-ascii endoding ?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reverse htmlspecialchars or something

Post by josh »

User avatar
Punkis
Forum Newbie
Posts: 12
Joined: Sat Sep 20, 2008 4:07 pm

Re: Reverse htmlspecialchars or something

Post by Punkis »

thank you my friend!
Post Reply