How can i put space between 2 Words [duplicate] - php

How do i simply write out a file including real tabs inside? tab means real tab which is not the spaces. How to write the tab or what is the character for real tab?
For example here:
$chunk = "a,b,c";
file_put_contents("chunk.csv",$chunk);
What character should i use to get tabs instead of Commas (,) there?
In the output file, there should be real tabs between the seperated words.
Real Tabs means, a true wide space we get when we press the <tab> key in a text-editor.

The tab character is \t. Notice the use of " instead of '.
$chunk = "<html>\t<head>\t\t<title>\t</head>";
PHP Strings - Double quoted
If the string is enclosed in double-quotes ("), PHP will interpret
more escape sequences for special characters:
...
\t horizontal tab (HT or 0x09 (9) in ASCII)
Also, let me recommend the fputcsv() function which is for the purpose of writing CSV files.

Use \t and enclose the string with double-quotes:
$chunk = "abc\tdef\tghi";

This should do:
$chunk = "abc\tdef\tghi";

Related

Regex to replace only single line breaks with br but NOT inside specific HTML tags [duplicate]

Say I have text like the following text selected with the cursor:
This is a test.
This
is a test.
This is a test.
This is a
test.
I would like to transform it into:
This is a test. This is a test
This is a test. This is a test
In other words, I would like to replace single line breaks by spaces, leaving empty lines alone.
I thought something like the following would work:
RemoveSingleLineBreaks()
{
ClipSaved := ClipboardAll
Clipboard =
send ^c
Clipboard := RegExReplace(Clipboard, "([^(\R)])(\R)([^(\R)])", "$1$3")
send ^v
Clipboard := ClipSaved
ClipSaved =
}
But it doesn't. If I apply it to the text above, it yields:
This is a test. This is a test.
This is a test. This is a test.
which also removed the "empty line" in the middle. This is not what I want.
To clarify: By an empty line I mean any line with "white" characters (e.g. tabs or white spaces)
Any thoughts how to do this?
RegExReplace(Clipboard, "([^\r\n])\R(?=[^\r\n])", "$1$2")
This will strip single line breaks assuming the new line token contains either a CR or a LF at the end (e.g. CR, LF, CR+LF, LF+CR). It does not count whitespace as empty.
Your main problem was the use of \R:
\R inside a character class is merely the letter "R" [source]
The solution is to use the CR and LF characters directly.
To clarify: By an empty line I mean any line with "white" characters (e.g. tabs or white spaces)
RegExReplace(Clipboard, "(\S.*?)\R(?=.*?\S)", "$1")
This is the same as the above one, but counts whitespace as empty. It works because it accepts all characters except line breaks non-greedily (*?) up to the first non-whitespace character both behind and in front of the linebreaks, since the . does not match line breaks by default.
A lookahead is used to avoid 'eating' (matching) the next character, which can break on single-character lines. Note that since it is not matched, it is not replaced and we can leave it out of the replacement string. A lookbehind cannot be used because PCRE does not support variable-length lookbehinds, so a normal capture group and backreference are used there instead.
I would like to replace single line breaks by spaces, leaving empty lines alone.
If you want to replace the line break with spaces, this is more appropriate:
RegExReplace(Clipboard, "(\S.*?)\R(?=.*?\S)", "$1 ")
This will replace single line breaks with a space.
And if you wanted to use lookbehinds and lookaheads:
Strip single line breaks:
RegExReplace(Clipboard, "(?<=[^\r\n\t ][^\r\n])\R(?=[^\r\n][^\r\n\t ])", "")
Replace single line breaks with spaces:
RegExReplace(Clipboard, "(?<=[^\r\n\t ][^\r\n])\R(?=[^\r\n][^\r\n\t ])", " ")
For some reason, \S doesn't seem to work in lookbehinds and lookaheads. At least, not with my testing.
Clipboard := RegExReplace(Clipboard, "(\S+)\R", "$1 ")
I believe this will work:
text=
(
This is a test.
This
is a test.
This is a test.
This is a
test.
)
MsgBox % RegExReplace(text,"\S\K\v(?=\S)",A_Space)
#SingleInstance force
#v::
Send ^c
ClipWait
ClipSaved = %clipboard%
Loop
{
StringReplace, ClipSaved, ClipSaved, `r`n`r`n, `r`n, UseErrorLevel
if ErrorLevel = 0 ; No more replacements needed.
break
}
Clipboard := ClipSaved
return

php convert single quote to double quote for line break character

I have a string coming from a language file containing strings with the text in the current language.
$str = 'blabla\n\nmore blabla';
$str is going to be used in an textarea where the \n must be a linebreak
If I place it inside double quotes this works.
The problem is that $str will always be in single quotes. I've been Googling and searching this site. There are many similar questions, but I didn't manage to find a solution.
How can I convert my single-quoted string (with a literal "\n") to a doublequoted string (where "\n" is converted to a linebreak)?
$str = str_replace('\n', "\n", $str);

escaping slashes using php

I am trying to remove the escape sequences present in between my output. I am coding using php and I have output from the function and it contains escape sequences like \r and \n. How do I escape these stuff from output so that I get a proper output. The issue is that I am trying to add this output to a csv file, so it will take \n as next line.
Single quotes inhibit escape sequences. '\r' is two characters, \ and r; "\r" is CR.
Use str_replace function:
$your_string = "here is your string"
$your_string = str_replace("\n", "", $your_string);
$your_string = str_replace("\r", "", $your_string);
You need to use stripslashes and str_replace check them out Here and Here
This post indicates that keeping the newline in the field should work, so long as:
newlines inside cells are represented as \n (str_replace("\r\n", "\n", $cell))
cells are quoted
rows are terminated with \r\n
If you're not already I'd recommend using fputcsv(), as it will take care of the quoting for you.

Saving line breaks with preg_replace

I have this code for saving line breaks in text area input to database:
$input = preg_replace("/(\n)+/m", '\n', $input);
On examining the input in the database, the line breaks are actually saved.
But the problem is when I want to echo it out, the line breaks do not appear
in the output, how do I preserve the line breaks in input and also echo them
out. I don't want to use <pre></pre>.
You are replacing actual sequences of newlines in the $input with the literal two character sequence \n (backslash + n) - not a newline. These need to be converted back to newlines when read from the database. Although I suspect you intend to keep these actual newlines in the database and should be using a double quoted string instead...
$input = preg_replace('/(\n)+/m', "\n", $input);
Note that I have replaced the first string delimiters with single quotes and the second string with double quotes. \n is a special sequence in a regular expression to indicate a newline (ASCII 10), but it is also a character escape in PHP to indicate the same - the two can sometimes conflict.
I think PHP has the solution:
nl2br — Inserts HTML line breaks before all newlines in a string
Edit: You may need to replace CR / LF to make it work properly.
// Convert line endings to Unix style (NL)
$input = str_replace(array("\r\n", "\r"), "\n", $input);
// Remove multiple lines
$input = preg_replace("/(\n)+/m", '\n', $input);
// Print formatted text
echo nl2br($input);

How can characters " \n \t \r " be replaced with '-'?

How can characters " \n \t \r " be replaced with '-' ?
echo preg_replace('/\s/','-','\n\t\n\r\n');//output '\n\t\n\r\n' instead should be'-----'
Edit: I have dynamic content in real app like:
preg_replace('/\s/','-',$_Request['content']);
can I fix it by adding "" around variable?
preg_replace('/\s/','-',"$_Request['content']");
Edit2:
How can be string converted from format 'str' to format "str"?
Thanks
Well, two things. First, the problem is single quotes in your replacement string. Meta-Characters (\n\t\r, etc) are not processed inside of single quotes.
However, don't use a regex for this. There's no need for the complexity of the regex. Use
Either use str_replace:
echo str_replace(array("\r", "\n", "\t", "\v"), '-', "\r\n\t\r\v\n\t");
Or strtr:
echo strtr("\r\n\t\r\v\n\t", "\r\n\t\v", '----');
Edit: Ahh, now I see what you're getting at. You have a string with a literal \r\n\t\r\v\n\t in it, and want to replace them out. Well, you can do that via regex:
$regex = '/(\s|\\\\[rntv]{1})/';
$string = preg_replace($regex, '-', $_GET['content']);
Basically, it matches any space character, and any literal \ followed by either r, n, t or v...
If you are looking to replace the actual whitespace characters, you need to enclose the input string in double quotes (") so PHP converts the escape sequences for you:
echo preg_replace('/\s/', '-', "\n\t\n\r\n");
Else if the escape sequences occur literally (i.e. you see \n\t\n\r\n instead of line feed, tab, line feed, carriage return, line feed), you need to replace by the following character class (and keep single quotes (') on the input string):
echo preg_replace('/\\\\[rnt]/', '-', '\n\t\n\r\n');
You ought to be passing content through $_POST instead of $_GET, I don't know how PHP handles tabs, newlines and returns in GET variables.
You are using 's instead of "s. You should change your code to:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
See here: single-quoted and double-quoted.
http://www.php.net/manual/en/language.types.string.php
There's also a string method for that:
echo strtr($str, "\r\n\t\v ", "-----");
If you want to remove linebreaks but retain spaces, then remove the trailing and the fifth -.
Since you seemingly want literal \r and \n converted, you need to use a map (or even a regex) like:
echo strtr($str, array('\\r'=>"\r", '\\n'=>"\n", '\t'=>"\t", ' '=>"␣"));
// single quoted strings escaped twice for illustration
Try:
echo preg_replace('/\s/','-',"\n\t\n\r\n");
Note the double quotes on the string.
If you enclose a string with single quotes, special characters lose their special meaning:
echo preg_replace('/\s/','-',"\n\t\n\r\n");

Categories