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);
?>
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.
Related
I'm making a function that that detect and remove all trailing special characters from string. It can convert strings like :
"hello-world"
"hello-world/"
"hello-world--"
"hello-world/%--+..."
into "hello-world".
anyone knows the trick without writing a lot of codes?
Just for fun
[^a-z\s]+
Regex demo
Explanation:
[^x]: One character that is not x sample
\s: "whitespace character": space, tab, newline, carriage return, vertical tab sample
+: One or more sample
PHP:
$re = "/[^a-z\\s]+/i";
$str = "Hello world\nhello world/\nhello world--\nhellow world/%--+...";
$subst = "";
$result = preg_replace($re, $subst, $str);
try this
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
or escape apostraphe from string
preg_replace('/[^A-Za-z0-9\-\']/', '', $string); // escape apostraphe
You could use a regex like this, depending on your definition of "special characters":
function clean_string($input) {
return preg_replace('/\W+$/', '', $input);
}
It replaces any characters that are not a word character (\W) at the end of the string $ with nothing. \W will match [^a-zA-Z0-9_], so anything that is not a letter, digit, or underscore will get replaced. To specify which characters are special chars, use a regex like this, where you put all your special chars within the [] brackets:
function clean_string($input) {
return preg_replace('/[\/%.+-]+$/', '', $input);
}
This one is what you are looking for. :
([^\n\w\d \"]*)$
It removes anything that is not from the alphabet, a number, a space and a new line.
Just call it like this :
preg_replace('/([^\n\w\s]*)$/', '', $string);
I have a string as
This is a sample text. This text will be used as a dummy for "various" RegEx "operations" using PHP.
I want to select and replace all the first alphabet of each word (in the example : T,i,a,s,t,T,t,w,b,u,a,d,f,",R,",u,P). How do I do it?
I tried /\b.{1}\w+\b/. I read the expression as "select any character that has length of 1 followed by word of any length" but didn't work.
You may try this regex as well:
(?<=\s|^)([a-zA-Z"])
Demo
Your regex - /\b.{1}\w+\b/ - matches any string that is not enclosed in word characters, starts with any symbol that is in a position after a word boundary (thus, it can even be whitespace if there is a letter/digit/underscore in front of it), followed with 1 or more alphanumeric symbols (\w) up to the word boundary.
That \b. is the culprit here.
If you plan to match any non-whitespace preceded with a whitespace, you can just use
/(?<!\S)\S/
Or
/(?<=^|\s)\S/
See demo
Then, replace with any symbol you need.
You may try to use the following regex:
(.)[^\s]*\s?
Using the preg_match_all and implode the output result group 1
<?php
$string = 'This is a sample text. This text will be used as a dummy for'
. '"various" RegEx "operations" using PHP.';
$pattern = '/(.)[^\s]*\s?/';
$matches;
preg_match_all($pattern, $string, $matches);
$output = implode('', $matches[1]);
echo $output; //Output is TiastTtwbuaadf"R"uP
For replace use something like preg_replace_callback like:
$pattern = '/(.)([^\s]*\s?)/';
$output2 = preg_replace_callback($pattern,
function($match) { return '_' . $match[2]; }, $string);
//result: _his _s _ _ample _ext. _his _ext _ill _e _sed _s _ _ummy _or _various" _egEx _operations" _sing _HP.
I'm trying to remove all words of less than 3 characters from a string, specifically with RegEx.
The following doesn't work because it is looking for double spaces. I suppose I could convert all spaces to double spaces beforehand and then convert them back after, but that doesn't seem very efficient. Any ideas?
$text='an of and then some an ee halved or or whenever';
$text=preg_replace('# [a-z]{1,2} #',' ',' '.$text.' ');
echo trim($text);
Removing the Short Words
You can use this:
$replaced = preg_replace('~\b[a-z]{1,2}\b\~', '', $yourstring);
In the demo, see the substitutions at the bottom.
Explanation
\b is a word boundary that matches a position where one side is a letter, and the other side is not a letter (for instance a space character, or the beginning of the string)
[a-z]{1,2} matches one or two letters
\b another word boundary
Replace with the empty string.
Option 2: Also Remove Trailing Spaces
If you also want to remove the spaces after the words, we can add \s* at the end of the regex:
$replaced = preg_replace('~\b[a-z]{1,2}\b\s*~', '', $yourstring);
Reference
Word Boundaries
You can use the word boundary tag: \b:
Replace: \b[a-z]{1,2}\b with ''
Use this
preg_replace('/(\b.{1,2}\s)/','',$your_string);
As some solutions worked here, they had a problem with my language's "multichar characters", such as "ch". A simple explode and implode worked for me.
$maxWordLength = 3;
$string = "my super string";
$exploded = explode(" ", $string);
foreach($exploded as $key => $word) {
if(mb_strlen($word) < $maxWordLength) unset($exploded[$key]);
}
$string = implode(" ", $exploded);
echo $string;
// outputs "super string"
To me, it seems that this hack works fine with most PHP versions:
$string2 = preg_replace("/~\b[a-zA-Z0-9]{1,2}\b\~/i", "", trim($string1));
Where [a-zA-Z0-9] are the accepted Char/Number range.
I need to remove all the characters after the character which I select in a string.
Here is my string
$string = "Blow the fun in the sun.paste a random text";
$new_string = preg_replace("/paste.*/","",$string,-1,$count);
echo $new_string;
My output is Blow the fun in the sun.
If my string is like this
$string = "Blow the fun in the sun.paste \n a random text";
$new_string = preg_replace("/paste.*/","",$string,-1,$count);
echo $new_string;
My output is
Blow the fun in the sun.
a random text
But, I need my output as Blow the fun in the sun. even if there are \n or \t or some other special characters in my strings. How can I match this, while taking those special characters into consideration?
You will need s flag (DOTALL) to make DOT match new lines:
$new_string = preg_replace("/paste.*/"s, "", $string, -1, $count);
Without s flag your regex is not matching new lines as your input contains new lines and you want to replace string that contains new line as well.
How can I remove the remaining part of a string after certain characters like ?, #, &, %, = in PHP? Any ideas? I tried preg_replace(), but I couldn't figure it out.
Update, just realized I read it wrong. You're looking for stuff before, not after. Updated code:
$test_string = 'remember?forget';
preg_match('/([^?#&%=]+)/', $test_string, $matches);
$part_before_char = $matches[1];
After run, $part_before_char = 'remember'
This should work:
$str = "Hello World#somesuffixstr";
preg_match("/^(.*?[?#&%=]).*/", $str, $str);
echo $str[1];
// Should output "Hello World#"
About the regex pattern:
It searches for beginning of string (^), then for any character 0 or more times (which is group #1), then such a symbol like & or %, then any character zero or more times. It replaces the string with the characters matched in group #1.
$str = 'mystring#deletedpartofstring';
$str = preg_replace('/[?#&%=].+/', '', $str);