php echo add extra space - php

Input text :
Test one two three fourfive
Text is splitted by preg_split with additional processing and now result array is:
Test one
<span>two</span>
three
<span>four</span>
five
When I echo this array in loop I get this:
Test one two three four five
Four and five should be displayed together, without space.
In HTML source it looks like so:
Test one
<span>two</span>
three
<span>four</span>
five
Extra space is added after four.
When I directly write this in HTML
Test one <span>two</span> three <span>four</span>five
text is echoed correctly. It seems that new line in HTML add extra space. Does someone know what happens here?

According to W3C, newlines are whitespace characters and should be rendered as a space by browsers.
If you want to get rid of the space, use <pre> and format your text as needed, or modify your markup so you can left-float "five."

A newline is a space. Remove that and your space disappears.

Remove \n from your echo and it should be fine.
Just like OP suggest newline is whitespace too.

Related

Displaying html stored in a database with the appropriate line breaks

I have code written in a database in several languages, html, css, php, etc.
I want it to show on my page with the appropriate line breaks. Like this:
https://cdn.discordapp.com/attachments/668127780927701032/1016459728194375780/unknown.png
So, when I display html, it executes it instead of displaying it, which is normal.
So I use htmlspecialchar, except that I can't make a line break anymore, everything is displayed on one line. And I can't br out of this.
How do I do that?
Apparently your string contains literal \n characters, not newlines. So you need to replace that with <br>.
echo str_replace('\n', '<br>', htmlspecialchars($stmt['html']));

How does mysql store multiple blank spaces?

I have a PHP script that displays an HTML form that allows a user to enter data in a <textarea> and store it in MySQL.
The user (me) entered multiple lines of text in the <textarea> that included \n, \r, and 4 consecutive space characters (to indent a list I was making). For example:
first line of text:
second line of text:
(A) some task
(B) another task
When the form was submitted, the content was stored correctly in MySQL, including the hidden \r, \n, and the four space characters before (A) and (B).
However, var_dump()-ing the data in PHP shows this:
first line of text.\r\nsecond line of text:\r\n (A) some task\r\n (B)another task
The four space (U+0020) characters I entered do not appear between the \r\n and (A), there is only one space character. I even ran the data through a string to hex converter and it only showed one U+0020 before each (A) and (B) instead of four U+0020s. But, when I re-open the form to edit the data in the <textarea>, the content shows up correctly, just I had entered it originally, with the 4 spaces before the (A) and (B).
My scripts all are behaving correctly and there is no problem. I'm just wondering: How is MySQL and <textarea> able to detect the 4 spaces, but var_dump() only detects one space?
Here is what I have tried to detect the 4 spaces in PHP, with the data stored in a PHP $Variable:
var_dump($Variable)-ing immediately before and after storing the data in MySQL, before/after stripslashes(), and before/after outputting the data to <textarea> and all each var_dump() does not detect detect the 4 spaces.
strpos($Variable) detects the 4 spaces.
print_r($Variable) does not detect the 4 spaces.
echo $Variable does not detect any hidden characters, except for single spaces (not the 4 consecutive spaces)
When you var_dump a variable, it is shown the same HTML.
If you want to show the spaces, you can add white-space:pre-wrap.
The white-space CSS property determines how whitespace inside an element is handled. To make words break within themselves, use overflow-wrap, word-break, or hyphens instead.
With pre-wrap, sequences of whitespace are preserved. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

Php Split text from html text area

I am trying to split a string that i pass from a HTML textarea.
Enter the list of Ids: <textarea rows="4" cols="50" name="ids">
I get it in a php script as
$idlist=$_GET["ids"];
When I try to split it, I couldnt get it with explode. Tried out with multiple delimitiers that could possibly split this and then tried using preg_split. I gave this as my regex.
$ids=preg_split("/\s/",$idlist);
Now that i split, I get empty array elements in the $ids. And I couldnt match them like
$ids[$i]==""|" "|"\s"
1) How will the text in text area be passed? I am giving one Id per line in the text area. It doesnt not carry new line character but a single space.
2) Will preg_split split out even the delimitiers that one uses to split the string?
I know I am making a blunder but couldnt figure out where. Someone any thoughts?
If each ID is on a new line, you need to explode by the new line character \n...
$ids=preg_split("/\r|\n)*/", $idlist);
EDIT: Updated to support windows encoding due to it being sent as a GET not a POST..
Explanation:
Windows send new lines as \r\n so if it is a GET the you need to provide support for this...
Here you go - just using explode to break them down into an array based on the "\n" new line character. I know you mentioned this does not work, but tested it and it works fine for me:
<?php
if (isset($_GET['ids'])) {
$arrayOfIds = explode("\n", $_GET['ids']);
}
?>
<form method="get" action="">
<textarea name="ids"></textarea>
<button type="submit">Submit</button>
</form>
As you mentioned above, when typing ID's into the textarea field I just put them each on a new line.
Figured it out. There was a single space as well as a New line character in the string. Exploded it with \n and then trimmed it with trim() that solved the problem.
With a textarea you have to assume the user is stupid when you ask them to provide data. If you say "Provide an id on each line" assume they will not read it and use commas or assume they press ENTER 50 times at the end, or assume they pasted it in from MS Word. No offense to the user, but just assume they are a Jelly fish on a keyboard and at some point it manages to click "submit".
Taking that into account, (and I'm assuming here that your ids are numbers) it means that anything that isn't a number can be ignored. This makes worrying about new lines, be that \n\r or whatever, completely irrelevant as long as there is some kind of character between each id then that's all we need.
I'm going to change $_GET["ids"] to $_POST["ids"] because I'd be posting that sort of data.
// This will create a list of ids with NO care about what delimeter
// the user used regardless of what you'd requested.
// Replace all non-numeric characters with #
$ids = preg_replace("/[^0-9]/is","#",$_GET["POST"]);
// Remove recursive # and trim
$ids = preg_replace("/#{1,}/is","#",trim($ids,"#"));
// Create array ensuring ids isn't empty
// This also removes unique id entries
$ids = empty($ids)?array():array_unique(explode("#",$ids));
// Show array
echo "<pre>";
print_r($ids);
echo "</pre>";

PHP strings space wrapping

I am using a TAB delimited file to import data in MySQL with PHP. My problem is whenever i display large strings (which are imported from Tab delimited files ONLY) which have spaces in them they won't wrap inside DIV,table cells e.t.c.
For example a big name like:
Mario Mark Le Blanc De Cooper VII
won't wrap inside a small DIV or table cell and instead will overflow and overlap nearby areas like other table cells. The wrapping problem occurs ONLY with data inserted from the tab delimited file.
Now the weird thing is after i go to PHPMyAdmin and manually remove all its spaces and re-add them, the word WILL wrap normally. I tried to import data from both ANSI and UTF8 encoded files but nothing changed.
I checked the ASCII code of the space character and it is indeed space.
I also tried str_replace to replace the string's spaces with new spaces but the problem persists
What about using preg_replace ? This might help you:
$yournewvariable= preg_replace('/\s+/','',$youroldvariable)
Try this:
$string = 'Mario Mark Le Blanc De Cooper VII';
$text = str_replace("\t", '', $string);
echo $text;
Try removing the actual tabs in the code.

replacing single break tags for two using regex

Whilst being aware of the pitfalls/dangers of certain html manipulation with regex (instead of using say the PHP dom manipulator) I'm trying to achieve something that should be pretty simple and not that risky.
Basically I have some uncleaned html copy from a database that doesn't use paragraphs but line break tags to produce the effects of paragraphs. Sometimes though the user only entered content with a single break so that the text line returns but without a blank line appearing. In such instances and ONLY in such instances I want to replace that single <br> with two (<br><br>).
So as an example...
This is <br>a test<br><br>example!
would become
This is <br><br>a test<br><br>example!
Note how the second set of breaks is left alone as its already got 2 tags.
Simply replace one or more occurences of <br> with <br> :)
Replace what:
(<br>)+
Replace with:
<br><br>
You can use negative lookahead and lookbehind to solve this:
(?<!<br>)<br>(?!<br>)
See the example here: http://rubular.com/r/WYjoenH1SA
(?<!NOPREFIX)
(?!NOPOSTFIX)
The first part prevents from matching, if the NOPREFIX is present - the second one if NOPOSTFIX is present.

Categories