This question already has an answer here:
Can I change tab width (\t) in a PHP string?
(1 answer)
Closed 9 years ago.
Is it possible to change the indentation space for "\t" in PHP? Searched for a while now but can't find anything pointing to an answer. I know there are some libraries out there that can tidy up the output but I'd prefer to be able to set this anyway, and to keep it consistant with my HTML indentation. I wish to change this indentation so that it becomes 4 spaces instead of the default "tab" if possible.
« The tab width is not specified by PHP, but by the program which views it. If you view it in an editor, you can set the tab width in the editor. »
Can I change tab width (\t) in a PHP string?
Specifying Tab-Width?
A tab character is a tab character. It doesn't have a width, it's a tab character. The thing (editor, viewer, browser) that is rendering the tab character visually (by indenting content) decides the width. There's no PHP or other "setting".
If you want tabs from a text input to become 4 spaces you can do this:
$formatted_string = preg_replace("/\t/", " ", $string);
For this to work on a html page you might want to replace the spaces with four
Related
This question already has answers here:
Echo from MySQL database with spaces and line breaks?
(2 answers)
Closed 9 years ago.
i have a little problem with the text to be readed from my database.
After the user has confirmed their new post, it saves in the database like this ( like i want it to do).
but in the webpage, it will ignore these lines, and just echo out everything on the same line.
Here is a bit my source code:
$objekttekst=str_replace("\\r\\n", "<br>", $obj->innhold);
$objekttittel=$obj->tittel;
?>
<h2><?=$objekttittel?></h2>
<p><?=$objekttekst?></p>
could someone help me out? thanks
Use nl2br() function.
$objekttekst = nl2br($obj->innhold);
The input textarea is pre-formatted, which means that it will show any newlines that the user enters. However, HTML rendered (web browser) does not display any newlines from the input, unless newlines are explicitly inserted with tags such as <BR>.
You have several options here. For sure these three are not your only options, but they are the ones I have personally been using most often.
Form textarea with pre-formatted text
If you want to display the data (objekttekst) in a similar textarea where the input was given, you could do:
<h2><?=$objekttittel?></h2>
<p><textarea><?=$objekttekst?></textarea></p>
This would suit you best in a situation where the user needs a possibility to edit the entry.
Preformatting
If you want to display the text as it is, you can always surround it with <PRE>...</PRE>. That will show any newlines, indentations etc. Note that this will make the output use a fixed-width font such as Courier New.
Convert newlines to <BR> tags
Use function nl2br() as already mentioned in another answer. See: http://php.net/manual/en/function.nl2br.php for more information.
Additional note...
You might want to look into regular expressions, as in many cases you might want to do also some other modifications to your data before showing it in the HTML page. nl2br() will take care of newlines, but for other and more complex modifications you should learn regular expressions.
You can surround your string with <pre> tag instead of replacing \n with <br>
Example:
<?php
$objekttekst=$obj->innhold
$objekttittel=$obj->tittel;
?>
<h2><?=$objekttittel?></h2>
<p><pre><?=$objekttekst?></pre></p>
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.
in my current template, i have a large block of text that comes in from the user. the block is then split at the 600 character mark and placed into a specific div on the page, the rest of the block is placed near the bottom of the page. this works fine, but a problem arises when there are a lot of links added near the beginning of the post.
the <a href='...'></a> is being counted towards the total character count. i always want to split the block at 600.
is there an easy way for me to find out the length of the html characters? that way I can just add them to the split_length. or any other ideas? ways to ignore the html characters entirely?
edit:
i should be clear i WANT the links to stay...
Use PHP's function strip_tags() and then count the characters.
You can remove all the HTML code, and split your text at the exact count. For that, use:
string strip_tags ( string $str [, string $allowable_tags ] )
This function tries to return a string with all NUL bytes, HTML and
PHP tags stripped from a given str. It uses the same tag stripping
state machine as the fgetss() function.
See here for more info.
I've got the following issue with PHP and PostgreSQL.
In a table I added the following value, mark the spaces.
Things: 10 POLI
When I read this out with PHP it will become
Things 10 POLI
My simpified code (for an ideal world without errors) is:
$query = "SELECT stuff, thing, planets FROM 42 WHERE answer = '-'";
$result = pg_query($connection, $query);
$resultTable = pg_fetch_all($result);
Then with
echo "Things: $result[stuff]";
My question is, which step eliminates all the white spaces? And how to get these spaces back? I know that most people want to remove them, I want to keep them.
that is not a PHP issue, but a HTML issue, becauyse if you output with echo, you do in fact generate HTML code.
The HTML specification defines, that multiple consecutive spaces get rendered as only one space.
If you want to avoid this, wrap a <pre> tag around the string:
echo "<pre>Things: $result[stuff]</pre>";
That's because browser does not recognize more than one space, you can use this code to convert consective spaces to (space understood by browser)
$str = str_replace(' ', ' ', $origText);
Or alternatively wrap your text in <pre> tag if that suites your requirements as suggested in comments below.
It still keeps the original text layout (I mean the spacing, offsets, new line, paragraphs) while the text fragment is stored in MySql ('text' type) field - I can tell when I peer into it in my DB browser (Adminer:)
but it gets lost when I output it from the DB: it becomes a single line string of my text characters. How can one restore it its original layout?
I've tried to reshape the text fragment using the PHP nl2br() function with some success:
it brought back the newline breaks, but the text words positioning is not kept, everything
shifts to the left.
Thanks in advance for a good idea.
If you've got multiple spaces and things like that. e.g. for code. Then trying using the pre tag.
http://htmldog.com/reference/htmltags/pre
http://reference.sitepoint.com/html/pre
The html_entity_decode() function converts HTML entities to characters.
The syntax is:
html_entity_decode(string, [quotestyle], [character-set]);
You can refer example2.