1

Actually I want to convert the string that I enter in input text, and then compare to another array.. See scenario below:

First, I enter this type of string in input text: acgta Second, I want to replace the "acgta" to the "TGCAT" which is a=T, c=G, g=C

This is code:

$data = "acgta";
$s = str_split($data);

$d = array("a" => "T","g" => "C","c" => "G","t" => "A");

foreach($d as $key1 => $value1) {
        echo str_replace($key1,$value1,$data);}
0

4 Answers 4

4
$data = "acgta";
echo str_replace(array('a', 'c', 'g', 't'), array('T', 'G', 'C', 'A'), $data);

or

$data = "acgta";
echo strtr($data, 'acgt', 'TGCA');

Take a look at the PHP manual pages for:

Sign up to request clarification or add additional context in comments.

1 Comment

strtr is by far the best way for this sort of thing.
1

You want to use the strtr function. This is pretty self-explanatory if you read the PHP doc: http://www.php.net/manual/en/function.strtr.php

$data = "acgta";
$d = array("a" => "T","g" => "C","c" => "G","t" => "A");

echo strtr($data, $d);

Comments

0

It would be easier to use str_replace with array elements. See below:

$data = "acgta";
$fromArr = array('a', 'g', 'c', 't');
$toArr = array('T', 'C', 'G', 'A');

echo str_replace($fromArr, $toArr, $data);

Comments

0

You can simply use this:

$string = "acgta";
$from = array('a', 'g', 'c', 't');
$to = array('T', 'C', 'G', 'A');
$string = str_replace($from, $to, $string);

Or in a simple way:

$string = "acgta";
$string = str_replace(array('a', 'g', 'c', 't'), array('T', 'C', 'G', 'A'), $string);

4 Comments

+1 Lolz, no other way... Is there any?
Covered pretty good in here. Just a race to the finish all within a minute or two!
Rightly said... Speed matters! But I need quality!
@rid No use of using preg_replace for this simple stuff.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.