2011-12-30 5 views
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; 
} 

すべてのヘルプは高く評価されます。おかげ

答えて

1

は、私は非常によくJavaの知らないが、Pattern.matcher()はJavaの機能でreplaceAll()常にで使用されているように、これはPHPでpreg_replace()のように書くことができます。そして基本的にはそれです。

正規表現パターンをコピーするときは、区切り文字を追加するには、preg_replaceが必要です。その次に、式は互換性があります。

/** 
* 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) 
*/ 
function dashEncode($s) { 
    static $pattern = array(
     'dashes' => "--+",  // "--" => "-" 
     'blanks' => "\\s\\s+", // " " => " " 

     'hyphen' => "(?<=[^-\\s])-(?=[^-\\s])", // like "Zeta-Jones" 
     'dash' => "[\\s]-[\\s]|-[\\s]|[\\s]-", // like "Blade - The Last Installment" 
     'blank' => "\\s+" 
    ); 

    $matcher = function($name, $s, $with) use ($pattern) { 
     isset($pattern[$name]) || die("pattern '$name' undefined."); 
     return preg_replace("~{$pattern[$name]}~", $with, $s); 
    }; 

    $s = 
    $matcher('blank', 
     $matcher('hyphen', 
      $matcher('dash', 
       $matcher('dashes', 
        $matcher('blanks', trim($s), " ") // compress embedded whitespace " " => " " 
       , "-") // trim and compress multiple dashes "---" => "-" 
      , "---") // replace dash with surrounding white space => "---" 
     , "--") // replace single "-" => "--" 
    , "-"); // replace blanks with "-"$matcher('blanks', trim($s), " "); // compress embedded whitespace " " => " " 
    return $s; 
} 
+0

それは、これはグローバル関数の内部であまり意味がありませんので、あなたのどちらかが、それからクラスを作成したり、ネストされた関数呼び出しをアンロールする必要があり、実際にはポートではありませんポート – Yada

+0

していただきありがとうございます。私はちょうどそれをプレーするために、PHPで 'preg_replace'と' trim'であることを実証しました。 – hakre

関連する問題