在PHP中使用数组和preg_replace替换字母

时间:2015-02-24 13:57:37

标签: php regex preg-replace

我有以下代码:

        $qSeq        = "ATTA";
        $suchmuster  = array("/A/","/T/","/G/","/C/","/U/");
        $ersetzungen = array("T","A","C","G","A");
        $qSeq        = preg_replace($suchmuster, $ersetzungen, $qSeq);
        echo $qSeq;

我的目标是,即ATTA成为TAAT,这种逻辑在Ruby中运行良好,但PHP有点会产生AAAA。知道如何使用PHP吗?

TNX!

1 个答案:

答案 0 :(得分:1)

如果您的替换可能需要更改您已更改的字母,请使用strtr()

$qSeq = 'CATGUT';
$suchmuster  = array("A","T","G","C","U");
$ersetzungen = array("T","A","C","G","A");
$qSeq        = strtr($qSeq, array_combine($suchmuster, $ersetzungen));
echo $qSeq;

给出

GTACAA