Skip to content

Instantly share code, notes, and snippets.

@zxteloiv
Created February 28, 2013 13:56
Show Gist options
  • Save zxteloiv/5056902 to your computer and use it in GitHub Desktop.
Save zxteloiv/5056902 to your computer and use it in GitHub Desktop.
reverse Chinese characters per line
#!/usr/local/php/bin/php
<?php
function cstrrev($str)
{
$len = strlen($str);
for($i = 0; $i < $len; $i++)
{
$char = $str{0};
if(ord($char) > 127)
{
$i++;
if($i < $len)
{
$arr[] = substr($str, 0, 2);
$str = substr($str, 2);
}
}
else
{
$arr[] = $char;
$str = substr($str, 1);
}
}
return join(array_reverse($arr));
}
$f = fopen("q.txt", "r");
while(!feof($f)) {
$line = fgets($f);
echo cstrrev(trim($line))."\n";
}
fclose($f);
@bunnywong
Copy link

非常好,一直也想寫这种程序… ^o^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment