note 77317 added to function.preg-replace

From: Date: Fri, 24 Aug 2007 10:10:37 +0000
Subject: note 77317 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-129746@lists.php.net to get a copy of this message
From what I can see, the problem is, that if you go straight and substitute all 'A's wit 'T's you can't tell for sure which 'T's to substitute with 'A's afterwards. This can be for instance solved by simply replacing all 'A's by another character (for instance '_' or whatever you like), then replacing all 'T's by 'A's, and then replacing all '_'s (or whatever character you chose) by 'A's: $dna = "AGTCTGCCCTAG"; echo str_replace(array("A","G","C","T","_","-"), array("_","-","G","A","T","C"), $dna); //output will be TCAGACGGGATC Although I don't know how transliteration in perl works (though I remember that is kind of similar to the UNIX command "tr") I would suggest following function for "switching" single chars: function switch_chars($subject,$switch_table,$unused_char="_") { foreach ( $switch_table as $_1 => $_2 ) { $subject = str_replace($_1,$unused_char,$subject); $subject = str_replace($_2,$_1,$subject); $subject = str_replace($unused_char,$_2,$subject); } return $subject; } echo switch_chars("AGTCTGCCCTAG", array("A"=>"T","G"=>"C")); //output will be TCAGACGGGATC ---- Server IP: 217.160.72.57 Probable Submitter: 84.137.154.142 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/77317 Del: integrated -- https://master.php.net/note/delete/77317/integrated Del: useless -- https://master.php.net/note/delete/77317/useless Del: bad code -- https://master.php.net/note/delete/77317/bad+code Del: spam -- https://master.php.net/note/delete/77317/spam Del: non-english -- https://master.php.net/note/delete/77317/non-english Del: in docs -- https://master.php.net/note/delete/77317/in+docs Del: other reasons-- https://master.php.net/note/delete/77317 Reject -- https://master.php.net/note/reject/77317 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129746) next »