Trying to remove new lines and spaces using regex - php

I am attempting to remove some line breaks and spaces from a multiline string I have, such as the following:
Toronto (YTZ)
to
Montreal (YUL)
I tried doing:
$matched = preg_replace('/[\n]/', '', $string);
var_dump($matched);
but all it returns is:
Montreal (YUL)
I've tried all sorts of combinations of regular expressions, but it only ever seems to find what I specify, replace it, and display anything AFTER the matched expression.
I'm sure it's something simple, but I can't seem to figure it out.
Thanks in advance!

\n only represents "go to line" if it is between double quotes in PHP "\n"... Your regex should be "/[\n]/" not '/[\n]/'
Anyway, don't use a regular expression for that, but str_replace("\n",'',$string) instead. It's faster.

As Kash already noticed you, expression of new line in different OS can be different.
That's where PHP_EOL constant is used. This constant is defined depending on OS.
$string = str_replace(PHP_EOL, '', $string);
if string could be created on different machine, then it would be better to replace "\r" and "\n" separately
$string = str_replace(array("\r", "\n"), '', $string);

$str = preg_replace('/\n+(?=.)/', " ",
preg_replace('/^\s*/m', "",
$str));
Check this code here.

Related

Preg Replace with Spaces but only single space

Okay so consider the following string:
"Hello How are you??"
I'd like it to return:
"Hello_How_are_you"
But my preg_replace is this:
preg_replace("/[^A-Za-z0-9]/","_",$string);
Which returns the following:
"Hello____How are you"
Whilst good, it gets rid of the foreign characaters but leaves a long line of ___ which looks ugly. I understand why, because it is replacing the spaces with _ which I what I asked it to do. However i'd like to to only output the one _ where it's replaced things.
How can I achiieve this, is it done in the regex or some other way?
Just add a quantifier: +. + means at least one of what was before, grab as many possible:
preg_replace("/[^A-Za-z0-9]+/","_",$string);
This can be an alternative regex.
$str = "Hello How are you??";
$op = preg_replace("/\s+/", "_", $str); // suggested by #Toto
//(or)
$op = preg_replace("/[\s]{1,}/", "_", $str);

How to remove every second occurrence within a string?

Basically, I have a string that I need to search through and remove every SECOND occurrence within it.
Here is what my string looks like ($s):
question1,answer1,answer2,answer3,answer4
question2,answer1,answer2,answer3,answer4
question3,answer1,answer2,answer3,answer4
Here is what my code currently looks like:
$toRemove = array("\n");
$finalString = str_replace($toRemove, "", $s);
As you can see, each line within my s string contains two \n between them. I would like to search through my string and only replace every SECOND \n so that my string ends up being:
question1,answer1,answer2,answer3,answer4
question2,answer1,answer2,answer3,answer4
question3,answer1,answer2,answer3,answer4
Is this possible? If so, how can I do it?
In your specific case, you may want to just replace two newlines with one newline:
$string = str_replace("\n\n", "\n", $string);
More complicated regex solutions could collapse any number of concurrent newlines:
preg_replace("/\n+/", "\n", "foo\n\nbar\n\n\n\n\nblee\nnope");
Adam's answer is correct for UNIX like systems but in Windows you can have different line endings. My Regex is a little bit rusty but I think this should work for UNIX and Windows.
$string = preg_replace('/[\n\r]{2}/', '\n', $string); Replace exact 2 line endings
$string = preg_replace('/[\n\r]+/', '\n', $string); Replace 1 or more line endings

Replace only certain '%' in a string

I am using str_replace() to replace all the slashes in a string with '%' and then I run the newly edited string through a function. One of the things this function does is adds '%%%' to the string.
Now, I need to replace only the percentage signs that I replaced before, that were slashes before, with slashes again.
So if the original string looks like this: 'the/wuick/bornw/foc/jumps/over/the/lazy/dog.'
After passing it through the function it will look like this: 'The%quick%brown%fox%jumps%over%the/lazy%dog.%%%'
And then after putting it through the part I need help with, it will look like this: 'The/quick/brown/fox/jumps/over/the/lazy/dog.%%%'
I would greatly appreciate any and all help replacing only the % that I had done with str_replace() before.
To replace single % signs with slashes, you can use
$result = preg_replace('/(?<!%)%(?!%)/', '/', $subject);
This uses negative lookaround assertions to make sure only those % signs are matched that are neither preceded nor followed by another % sign.
See it on regex101.com.
without too many complications execute in this order:
$str = 'The%quick%brown%fox%jumps%over%the/lazy%dog.%%%';
$str= str_replace('%%%','***triple_percent**', $str);
$str= str_replace('%','/', $str);
$str= str_replace('***triple_percent**','%%%', $str);
Idealy first see why you have so many %, I am sure you can simplify your functions.
Another solution is using regular expressions like Tim says in his answer
$str= preg_replace('/(?<!%)%(?!%)/', '/', $str);
Break apart means:
(?<!%) Not % before
% find %
(?!%) Not % after
also add g so it can find it many times, and i for case sensitive in the case that you might need it:
$str= preg_replace('/(?<!%)%(?!%)/ig', '/', $str);
Add another replate with blank string like:
$string = str_replace('%', '', $string);

Problem Replacing Literal String \r\n With Line Break in PHP

I have a text file that has the literal string \r\n in it. I want to replace this with an actual line break (\n).
I know that the regex /\\r\\n/ should match it (I have tested it in Reggy), but I cannot get it to work in PHP.
I have tried the following variations:
preg_replace("/\\\\r\\\\n/", "\n", $line);
preg_replace("/\\\\[r]\\\\[n]/", "\n", $line);
preg_replace("/[\\\\][r][\\\\][n]/", "\n", $line);
preg_replace("/[\\\\]r[\\\\]n/", "\n", $line);
If I just try to replace the backslash, it works properly. As soon as I add an r, it finds no matches.
The file I am reading is encoded as UTF-16.
Edit:
I have also already tried using str_replace().
I now believe that the problem here is the character encoding of the file. I tried the following, and it did work:
$testString = "\\r\\n";
echo preg_replace("/\\\\r\\\\n/", "\n", $testString);
but it does not work on lines I am reading in from my file.
Save yourself the effort of figuring out the regex and try str_replace() instead:
str_replace('\r\n', "\n", $string);
Save yourself the effort of figuring out the regex and the escaping within double quotes:
$fixed = str_replace('\r\n', "\n", $line);
For what it is worth, preg_replace("/\\\\r\\\\n/", "\n", $line); should be fine. As a demonstration:
var_dump(preg_replace("/\\\\r\\\\n/", "NL", 'Cake is yummy\r\n\r\n'));
Gives: string(17) "Cake is yummyNLNL"
Also fine is: '/\\\r\\\n/' and '/\\\\r\\\\n/'
Important - if the above doesn't work, are you even sure literal \r\n is what you're trying to match?..
UTF-16 is the problem. If you're just working with raw the bytes, then you can use the full sequences for replacing:
$out = str_replace("\x00\x5c\x00\x72\x00\x5c\x00\x6e", "\x00\x0a", $in);
This assumes big-endian UTF-16, else swap the zero bytes to come after the non zeros:
$out = str_replace("\x5c\x00\x72\x00\x5c\x00\x6e\x00", "\x0a\x00", $in);
If that doesn't work, please post a byte-dump of your input file so we can see what it actually contains.
$result = preg_replace('/\\\\r\\\\n/', '\n', $subject);
The regex above replaces the type of line break normally used on windows (\r\n) with linux line breaks (\n).
References:
Difference between CR LF, LF and CR line break types?
Right way to escape backslash [ \ ] in PHP regex?
Regex Explanation
I always keep searching for this topic, and I always come back to a personal line I wrote.
It looks neat and its based on RegEx:
"/[\n\r]/"
PHP
preg_replace("/[\n\r]/",'\n', $string )
or
preg_replace("/[\n\r]/",$replaceStr, $string )

What's the cross-platform regex to replace a line ending?

I've got a string like "foo\nbar", but depending on platform, that could become "foo\n\rbar", or whatever. I want to replace new lines with ", ". Is there a nice (php) regex that'll do that for me?
Try the regular expression (?:\r\n|[\r\n]):
preg_replace('/(?:\r\n|[\r\n])/', ', ', $str)
You dont want to use a Regex for a simple replacement like this. Regular string replacement functions are usually much faster. For the line break, you can use the OS aware constant PHP_EOL, e.g.
str_replace(PHP_EOL, ', ', $someString);
On Windows, this will replace \r\n. On Mac \r and on all other systems \n.
Wouldn't str_replace(array("\n", "\r"), "", $string) work?

Categories