0

my text file is like this:

atagatatagatagtacataacta\n
actatgctgtctgctacgtccgta\n
ctgatagctgctcgctactacgat\n
gtcatgatctgatctacgatcaga\n

I need this file in single string or in single line in both same and reverese order like this:

atagatatagatagtacataactaactatgctgtctgctacgtccgtactgatagctgctcgctactacgatgtcatgatctgatctacgatcaga

and "reverese" (for which I didn't write code because I need help ).
I am using:

<?php
    $re = "/[AG]?[AT][AT]GAGG[ATC]GC[GA]?[ATGC]/"; 

    $str = file_get_contents("filename.txt");
    trim($str);

    preg_match($re, $str, $matches);
    print_r($matches);
?>
1
  • Why do you use preg_match? Just remove the newline characters. Commented Mar 10, 2015 at 6:56

1 Answer 1

1

You can remove spaces and newlines using preg_replace, and you can reverse a string using strrev.

$yourString = "atagatatagatagtacataacta\n actatgctgtctgctacgtccgta\n ctgatagctgctcgctactacgat\n gtcatgatctgatctacgatcaga\n";    
$stringWithoutSpaces = preg_replace("/\s+/", "", $yourString);
$stringReversed = strrev($stringWithoutSpaces);
echo $stringReversed;

http://php.net/manual/de/function.preg-replace.php

http://php.net/manual/en/function.strrev.php

Explanation: With preg_replace you replace any character in $yourString with an empty string "" that matches the search pattern "/\s+/". The \s in the search pattern stands for any whitespace character (tab, linefeed, carriage return, space, formfeed), the + is there to match also multiple whitespace characters, not just one.

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

1 Comment

By the way, in your example you use /n as newlines, which will not work, you have to use \n. I made an update in my example to correct this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.