How to remove ANSI-Code (" ") from string in PHP - php

I have tried a lot but nothing is working. I want to import a XML-file with PHP. In some strings the customer puts some ANSI-Code Carrier Returns ("
"). I have tried to remove them with:
str_replace('\r', '', $xml->description);
I also tried it with "&\#13;", "\r\n", "\&\\#13\;" in the search but nothing works. Do you have any idea how to remove these linebreaks?
Thanks!

Since your XML processor is already handling de-entitying the entities, you'll be left over with plain ASCII \n or \r or \r\n. PHP does not handle \r or \n inside of single quotes. It only translates them to their respective characters (codes 10 and 13), when the \r and \n are inside of double quotes.
You just need to use "\n" or maybe "\r\n".

Should just be a simple case of:
str_replace('
', '', $xml->description);
Notice I haven't escaped the # with a \.

Actually, This runs just fine
$str = "&\#13;"; //just an example
echo str_replace("&\\#13;", "hello", $str);
Demo

This worked for me:
str_replace("\x13", '', $str);
I'm using the hexcode for that char.

Related

php remove the carriage return on an assigned variable

If I assign a 2 lines value to a variable like this.
$tt = 'part 1
part 2 ';
and I echo it.
echo $tt;
My browser will display it on 2 lines and it's fine. But on the source page, the code will have it on 2 lines.
How can it get rid of this carriage return.
Since it',s not a chr(13) and a \r, these don't works.
echo str_replace(chr(13), '-', $tt);
echo str_replace('\r', '-', $tt);
Any idea?
It really depends on the environment you're in. In Windows a line break is \r\n. In *nix environments it's just \n.
echo str_replace("\r", "-", str_replace("\n", "-", $tt));
Simple way for Windows or Linux:
str_replace("\r\n", "-", $var);
On Linux, the \r\n still gets detected. Anything else suggested here is overkill (though not necessarily incorrect).
Keep in mind that you need to use double quotes when using special characters like \r or \n.
Edit:
If you want to be extra careful, you can use this instead:
str_replace(array("\r\n", "\n"), "-", $var);
You shouldn't need any extra overhead of using the char() function, for something like \r or \n.
You can try
str_replace(array(chr(13),chr(10)), "-", $tt);

Trying to remove new lines and spaces using regex

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.

split function not working on new lines

Hi I have simple split script and I am splitting a string on new line that has many new lines characters in it. Here is the code :
<?php
$string = $argv[1]; // CASE 1: COMMAND LINE ARGUMENT.
echo "String is : $string\n";
//$string = 'Hello Shakir\nOkay Shakir\nHow are you ?'; //CASE2: SINGLE QUOTE.
$string = "Hello Shakir\nOkay Shakir\nHow are you ?"; //CASE 3: DOUBLE QUOTE.
$lines = array();
$lines = split("\n", $string);
foreach ( $lines as $line ) {
echo "line is : $line\n";
//var_dump($line);
}
?>
It works fine when I use CASE 3 in the code, but it doesnt work when I use either CASE1 or CASE 2 (Only CASE 3 works fine). Can anybody please shed some light on this ?
This is how I run it on command line(linux machine) :
php my_script.php "Hello Shakir\nOkay Shakir\nHow are you ?"
In this case when I print $argv[1], it prints the entire string but it treats it same as CASE2 (with single quotes).
UPDATE :
Many of you have said what the cause of the issue is and not the answer to it. However, knowing the cause helped me fix it. So the answer is :
ANSWER :
Instead of using \n in double quotes ("\n"), use single quotes ('\n') :
$lines = split('\n', $string);
OR
$lines = explode('\n', $string);
However split counts '\' also as a character and I dont know why. But explode is correct. Since split is deprecated I dont have done much research on this.
Thank you for all who let me know that split is deprecated.
CASE1 and CASE2 will not work for a simple reason, because \n is evaluated as a literal \n and not a newline character.
Only CASE3, with the double quotes, will evaluate \n as a newline.
Also, the function split() is deprecated. Try using explode() instead.
That's because '\n' not outputs new line, and not interpritate as new line, so in CMD too you don't pass new lines;
Also, split() is deprecated, use explode();
The big difference between using single quotes '' and double quotes "" are for automatic replacement inside of strings.
Using "" will enable replacement of variables and usage of escape sequences while '' doesn't support this.
$name = 'Mathieu';
$case1 = "Hi this is $name speaking\nPleased to meet you!";
echo $case1;
//Will result in
Hi this is Mathieu speaking
Pleased to meet you!
While using single quotes will yield:
$name = 'Mathieu';
$case1 = 'Hi this is $name speaking\nPleased to meet you!';
echo $case1;
//Will result in
Hi this is $name speaking\nPleased to meet you!
All escape sequences possible are:
\n Line feed (dec 13)
\r Carriage return (dec 10)
\t Tab (dec 8)
Relative to your question about line feeds, note that \n, \r, \r\n are using in different combination depending on the OS and information coming from a Windows OS usually features \r\n while linux only has \n. MacOS used to or still features only \r i think, not sure.
Single-quoted strings are not supposed to expand all the escape strings like /n.

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 )

newline question

I want to detect a carriage return or a newline character when a user enters data into a textarea. What is the best way to handle this? I've tried str_replace with escape characters but carriage returns and newlines are not detected.
OK, say I type the following into a textarea:
The summer was hot this year
but next year is supposed to be cooler.
I want to detect the CRs. In this case, there is one.
Newlines could be \r, \r\n, or \n, depending on the client.
$input = preg_replace('/\r\n?/',"\n",$input)
will standardize all of your newlines to "\n" regardless of where they came from.
You can do it like this with str_replace:
function replace_newline($string) {
return (string)str_replace(array("\r", "\r\n", "\n"), '', $string);
}
There are several ways how new line is stored.
Some systems use only "\n" some "\r" and some both "\r\n". You need to check for both "\r" and "\n"
Try the following. It's always worked a charm for me.
You need to replace \n AND \r, it's because a linux system and a windows system use different characters for newlines.
$input = str_replace(array("\n","\r"),'',$input);
Or check for chr(10) and replace on that
Have you tried preg_replace because that can be used for regex replacements and then you can replace using \n or \r or any combination you require although I believe str_replace should also work fine.
function replace_newlines($string) {
return preg_replace('/\r\n|\r|\n/', '', $string);
}

Categories