Dash Encoding将java函数转换为PHP

时间:2011-12-30 00:02:18

标签: java php regex encoding

我正在尝试将java函数转换为等效的PHP函数。

爪哇:

/**
 * 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 个答案:

答案 0 :(得分:1)

我不太了解java,但由于Pattern.matcher()与java函数中的replaceAll() 始终一起使用,因此可以将其写为preg_replace()用PHP。基本上就是这样。

复制正则表达式模式时,添加分隔符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;
}