Archive for the ‘PHP’ Category

h1

CJK Characters Displaying Incorrectly

July 8, 2008

When I tried retrieving something with CJK characters from my database (e.g. こんにちは!), they displayed as ???? characters. But when I checked the actual database, the characters were written as in CJK characters (こんにちは!). I looked for a solution and I found out that I need to add this line below my database connection:

mysql_query("SET NAMES 'utf8'");

So, it would probably look something like this:

$dbconnect = mysql_connect('localhost', 'username', 'password');
if (!$dbconnect) die(mysql_error());
mysql_select_db('dbname');

mysql_query(“SET NAMES ‘utf8′”);

An ultra short summary explanation for this, according to that long article I read, MSQL databases tend to have a latin1 charset set as a default and it needs to be changed to utf8 to make it work.

Another thing is that in your database table’s Collation, you should find a utf8_general_ci instead of a latin1_swedish_ci one.

PS: I love you google!

h1

Additional Array Field

December 9, 2007

You can add a new array field in the existing array result of the query using PHP.

Original Array Result:

[0] => Array (
[f_user_id] => 20250
[f_username] => yoshikihayashi
[f_firstname] => Yoshiki
[f_lastname] => Hayashi
[f_added] => 2006-08-25 16:39:00
[f_raffle_number] => 1543833569
[f_date] => 2007-12-18 13:00:59
[f_birthdate] => 1965-11-20
[f_sex] => M
)
[1] => Array (
[f_user_id] => 1026318
[f_username] => gacktcamui
[f_firstname] => Gackt
[f_lastname] => Camui
[f_added] => 2007-12-09 18:16:48
[f_raffle_number] => 1215771422
[f_date] => 2007-12-18 13:00:59
[f_birthdate] => 1968-07-04
[f_sex] => M
)

Procedure:

for($i=0;$i<count($pagevars['winnersbuy']);$i++):
$bday = $pageVars['winnersBuy'][$i]['f_birthdate'];
$age = $this->$birthday("$bday");
$pageVars['winnersBuy'][$i]['f_age'] = "$age";
endfor;

Output:

[0] => Array (
[f_user_id] => 20250
[f_username] => yoshikihayashi
[f_firstname] => Yoshiki
[f_lastname] => Hayashi
[f_added] => 2006-08-25 16:39:00
[f_raffle_number] => 1543833569
[f_date] => 2007-12-18 13:00:59
[f_birthdate] => 1965-11-20
[f_sex] => M
[f_age] => 42
)
[1] => Array (
[f_user_id] => 1026318
[f_username] => gacktcamui
[f_firstname] => Gackt
[f_lastname] => Camui
[f_added] => 2007-12-09 18:16:48
[f_raffle_number] => 1215771422
[f_date] => 2007-12-18 13:00:59
[f_birthdate] => 1968-07-04
[f_sex] => M
[f_age] => 35
)

h1

Truncates Words

August 26, 2007

This code truncates the sentence into specified number of words.

//-- truncates entries
$entry = "This is where the long entry is placed.";
$words = split(" ",$entry);
$excerpt = join(" ",array_slice($words,0,50));
echo $excerpt;
echo " [more]";


From my codix.

h1

Read Line Breaks in Post

August 26, 2007

To read line breaks (\n) to post, use:

nl2br($result['message']);


From my codix.

h1

Auto Hyperlink

August 26, 2007

Honestly, I don’t get all the\\ and the //.function auto_hyperlink($hyperlink) {
$href_link = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $hyperlink);
$mailto_link = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\">\\1</a>", $href_link);
return($mailto_link);
}
$true_msg = auto_hyperlink($true_msg);


From my codix.

h1

Truncates Sentences

August 26, 2007

This code truncates a paragraph to specified sentences:

$entry = "This is the first sentence. And the second sentence. The third sentence. And the fourth.";
$sentence = preg_split("/[\.\!\?]/", $entry);
for($i=0; $i<count($sentence);>
echo("$sentence[$i]<br />\n");
}</count($sentence);>


From my codix.

h1

Readable Date

August 26, 2007

To make dates from whatever format to human readable format:

$readable_date = date("F d, Y", strtotime($news['news_date']));
echo $readable_date;


From my codix.