0
java関数を同等のPHP関数に変換しようとしています。ダッシュエンコーディングからJava関数をPHPに変換する
のJava:
/**
* Dash Encoding:
*
* Zeta-Jones <=> Zeta--Jones
* Blade - The Last Installment <=> Blade---The-Last-Installment
* Wrongo -Weird => Wrongo---Weird (decodes to => Wrongo - Weird)
* Wrongo- Weird => Wrongo---Weird (decodes to => Wrongo - Weird)
*/
private static Pattern dashes = Pattern.compile("--+"); // "--" => "-"
private static Pattern blanks = Pattern.compile("\\s\\s+"); // " " => " "
private static Pattern hyphen = Pattern.compile("(?<=[^-\\s])-(?=[^-\\s])"); // like "Zeta-Jones"
private static Pattern dash = Pattern.compile("[\\s]-[\\s]|-[\\s]|[\\s]-"); // like "Blade - The Last Installment"
private static Pattern blank = Pattern.compile("\\s+");
public static String dashEncode(String s) {
if (s == null) return s;
s = blank.matcher(
hyphen.matcher(
dash.matcher(
dashes.matcher(
blanks.matcher(s.trim()).replaceAll(" ") // compress embedded whitespace " " => " "
).replaceAll("-") // trim and compress multiple dashes "---" => "-"
).replaceAll("---") // replace dash with surrounding white space => "---"
).replaceAll("--") // replace single "-" => "--"
).replaceAll("-"); // replace blanks with "-"
return s;
}
これまでのところ私が持っている:
PHP
function dashEncode($str) {
// replace blanks with "-"
$str = str_replace(' ', '-', $str);
// replace single "-" => "--"
$str = str_replace('-', '--', $str);
return $str;
}
すべてのヘルプは高く評価されます。おかげ
それは、これはグローバル関数の内部であまり意味がありませんので、あなたのどちらかが、それからクラスを作成したり、ネストされた関数呼び出しをアンロールする必要があり、実際にはポートではありませんポート – Yada
していただきありがとうございます。私はちょうどそれをプレーするために、PHPで 'preg_replace'と' trim'であることを実証しました。 – hakre