How to delete string with regex preg_replace php - php

i have full string like this:
Route::post('asdasdasdad/{param1}/{param2}', 'Admin\RouteController#a212e12e');.
and want to delete that route so in preg_replace i focus on
Route::post('asdasdasdad as start text and
Admin\RouteController#a212e12e'); as last text.
here what i try
preg_replace("/Route::post('asdasdasdad\(.*Admin\RouteController#a212e12e');\s*/s", "", $string);
but its not working.

you have some errors in your regex, some un-escaped regex characters. try this
preg_replace("/Route::post\('asdasdasdad.*Admin\\\\RouteController#a212e12e'\);\s*/s", "", $string);
if you want to replace multiple lines in one go
preg_replace_all("/Route::post\('asdasdasdad.*Admin\\\\RouteController#a212e12e'\);\s*/s", "", $string);
witch works as if you add the multi line modifier to your regex
$string = file_get_contents('route.php');
$string = preg_replace("/Route::post\('asdasdasdad.*Admin\\\\RouteController#a212e12e'\);\s*/s", "", $string);
echo $string;
you get the line with EOL removed

Related

Remove white spaces from the left and right sides of a string on PHP

I always have problems with strings full of weird characters that are not white spaces but they do count as an element of the string. How can I remove all this characters from the string (not removing inner spaces)?
I am using preg_replace, but it eliminates inner spaces which I want to keep.
$string = preg_replace('/\s+/', '', $string);
Php shows that "My string" has 40 elements
string(40)=>"
My string
"
And it should have only 9 just like that:
string(9)=>"My string"
This spaces at the beginning and end of the word are not feed, enter or tab since I've used string replace just like that:
str_replace("\r", "", $string);
str_replace("\t", "", $string);
str_replace(char(10), "", $string);
You can try the trim function
Example
$bad_text = "\t\tWhitespace is bad!!! ";
var_dump($bad_text);
$good_text = trim($bad_text);
var_dump($good_text);

Text file as single string in PHP code

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.

PHP Remove spaces and %20 within single function

I wish to remove white space from a string. The string would have ben urlencoded() prior, so I also wish to remove %20 too. I can do this using two separate functions, but how do i do this with one function?
$string = str_replace("%20","",$string);
$string = str_replace(" ","",$string);
You could use preg_replace function.
preg_replace('~%20| ~', "", $string)
Don't use a regex for that but strtr:
$result = strtr($str, array('%20'=>'', ' '=>''));

preg_replace: How to not remove the whole part?

I got this string:
$string = "hello123hello237ihello523";
I want to search for an "o" followed by a number. Then I want to remove only the "o".
I've been trying:
preg_replace("/kl[0-9]/", "", $string);
The problem is that is removes the number as well. I only want to remove the "o". Any ideas?
use positive lookahead:
echo preg_replace("/o(?=[0-9])/", "", $string);
For more information: http://www.regular-expressions.info/lookaround.html
One other option is:
echo preg_replace("/o([0-9])/", "\\1", $string);
This will replace the o[number] with [number]
You could use back references:
echo preg_replace('/o([0-9])/', '$1', $string);
See the documentation for information on referencing subpatterns.

How to remove all non-alphanumeric and non-space characters from a string in PHP?

I want to remove all non-alphanumeric and space characters from a string. So I do want spaces to remain. What do I put for a space in the below function within the [ ] brackets:
ereg_replace("[^A-Za-z0-9]", "", $title);
In other words, what symbol represents space, I know \n represents a new line, is there any such symbol for a single space.
Just put a plain space into your character class:
[^A-Za-z0-9 ]
For other whitespace characters (tabulator, line breaks, etc.) use \s instead.
You should also be aware that the PHP’s POSIX ERE regular expression functions are deprecated and will be removed in PHP 6 in favor of the PCRE regular expression functions. So I recommend you to use preg_replace instead:
preg_replace("/[^A-Za-z0-9 ]/", "", $title)
If you want only a literal space, put one in. the group for 'whitespace characters' like tab and newlines is \s
The accepted answer does not remove spaces.
Consider the following
$string = 'tD 13827$2099';
$string = preg_replace("/[^A-Za-z0-9 ]/", "", $string);
echo $string;
> tD 138272099
Now if we str_replace spaces, we get the desired output
$string = 'tD 13827$2099';
$string = preg_replace("/[^A-Za-z0-9 ]/", "", $string);
// remove the spaces
$string = str_replace(" ", "", $string);
echo $string;
> tD138272099

Categories