print variable with alignment - php

I manually execute a query and copy paste the output to an editor.Now it is well aligned like:
Hi,
This is sample data.
Thanks.
If I stored the output of the query in a php varibale and print it using echo command it is not aligned. It looks like
Hi, This is sample data. Thanks.
Is it possible to print the output as it is in php?

you need <pre> tag for that. but I suggest you should format it with proper html
echo "<pre>".$youvariable."</pre>";

PHP prints it perfectly fine. If you're looking at it in a browser though, consecutive spaces and newlines are collapsed into a single space by the browser. Replace newlines with <br> tags to keep them, using nl2br.

Related

How to change encoding from plain text to Unicode so that I can read special characters from a HTML?

Below is my code :
<?php
// example of how to use basic selector to retrieve HTML contents
include('/Library/WebServer/Documents/simple_html_dom.php'); //this is the api for the simplehtmldom
// get DOM from URL or file
$html = file_get_html('http:/www.google.hk');
// extract text from table
echo $html->find('td[align="top"]', 1)->innertext.'<br><hr>';
// extract text from HTML
echo $html->innertext;
?>
I am using the simplephphtmldon API. When I execute my php program in my local server instead I get so many unrecognized characters due to the fact that the plain text can't really encode them to show up like they supposed to. Can Someone tell me what i need to change to inner text in order to get all the characters to show up? PS i also did try plaintext without any luck. textContent seems broken to me. Perhaps i need to try a different element first (?). Thanks
echo utf8_encode($html->innertext);
Or
echo utf8_decode($html->innertext);
It depends on the original encoding, so you may want to try both.
Note:
If you're seeing the output on a browser, make sure you set Unicode as text encoding or use this the following code at the top of you script.
header('Content-Type: text/html; charset=utf-8');

new lines in PHP. how?

Code:
<?php
echo "Hello World!\nSecond line";
?>
I'm trying to display this as two separate lines but the line break character doesn't work and instead a space gets printed inbetween the two. I've tried \r\n as well as .PHP_EOL. as well as placing the string in single quotes. None of them seems to work. So how do I print new lines in PHP?
I'm working on phpDesigner 8
Use nl2br() to turn the new lines into HTML <br/> elements:
<?php
echo nl2br("Hello World!\nSecond line");
?>
The linebreaks are actually created but you just don't see them. Change the Content-Type header to see the original result:
header('Content-Type: text/plain');
This is very useful when debugging. You could also view the page source in your browser to see the original output.
And to answer your question, the most easiest way to do this would be to use nl2br() (as has been suggested in John's answer above).
As you are printing in HTML, you should use the <br /> tag instead of \n

(PHP+HTML) Codes inside PHP

I have some coding which has both PHP & HTML tags... it is a very big coding... now i want to add these coding inside PHP tag... i tried using "echo" statement and adding quotes, concatenation... it doesnt work and showing some error
here is a sample line..
<td>Developer</td><td>:</td><td><?php echo wpws_get_content($url,'.doc-header-link','','user_agent=FairAndroid.com&on_error=error_hide&cache=43289&callback=call')?></td>
the above line has lots of quotes, html and php... i have lots of lines like this...
what is the best way to include (HTML & PHP) inside PHP ??
there's nothing wrong with your line. Follow error you're seeing and try to correct them
http://sandbox.phpcode.eu/g/3a329.php
"adding quotes, concatenation" That's what you need, but it's kind of error-prone to reformat long lines:
echo '<td>Developer</td><td>:</td><td>'
. wpws_get_content($url,'.doc-header-link','','user_agent=FairAndroid.com&on_error=error_hide&cache=43289&callback=call')
. '</td>';
This is just a suggestion so that no matter how long your html codes just a simple include will simply insert it to your php file.
Put you html codes in a different file
Then just include it in your php file.
file.html
<p>Hello World!</p>
file.php
<?php
include("file.html");
?>
Assuming you want to print that chunk of code, you failed to escape the quotes and the special characters ( <, >, & and so on).
Splitted on multiple lines for better clarity.
<?php
echo('<td>Developer</td>');
echo('<td>:</td>');
echo('<td><?php echo wpws_get_content($url, \'.doc-header-link\', \'\', \'user_agent=FairAndroid.com&on_error=error_hide&cache=43289&callback=call\')?></td>');
?>

PHP: How to prevent unwanted line breaks

I'm using PHP to create some basic HTML. The tags are always the same, but the actual links/titles correspond to PHP variables:
$string = '<p style="..."><strong><i>'.$title[$i].'</i></strong>
<br>';
echo $string;
fwrite($outfile, $string);
The resultant html, both as echoed (when I view the page source) and in the simple txt file I'm writing to, reads as follows:
<p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>
While this works, it's not exactly what I want. It looks like PHP is adding a line break every time I interrupt the string to insert a variable. Is there a way to prevent this behavior?
Whilst it won't affect your HTML page at all with the line breaks (unless you are using pre or text-wrap: pre), you should be able to call trim() on those variables to remove newlines.
To find out if your variable has a newline at front or back, try this regex
var_dump(preg_match('/^\n|\n$/', $variable));
(I think you have to use single quotes so PHP doesn't turn your \n into a literal newline in the string).
My guess is your variables are to blame. You might try cleaning them up with trim: http://us2.php.net/trim.
The line breaks show up because of multi-byte encoding, I believe. Try:
$newstring = mb_substr($string_w_line_break,[start],[length],'UTF-8');
That worked for me when strange line breaks showed up after parsing html.

Textarea Adding Space To Beginning of Text?

I'm having a strange problem in that I have php inserting text into a <textarea> and the <textarea> is adding one white space to the top of my text.
I created an example page to display the problem... here it the code behind the page.
<textarea style="width:600px;height:100px;"><?php get_film_info('main description'); ?></textarea>
<br>
<textarea id="mainDescription style="width:600px;height:100px;">Text just typed in</textarea>
<br>
<?php get_film_info('main description'); ?>
You can see that without the <textarea> tag, the text does not include the indent. My database also reflects no indent, as well as the php output outside of the <textarea>...
Any clue what could be going on?
the sample page
Edit: You were all right, of course I didn't bother checking the source code of the output file. Turns out when I was adding the data (via ajax) I was sending my data like var data = '&main_description= ' + mainDescription. Notice the space between the "=" and the "+".
Thank you all for your input, gotta just give the check mark to the guy on the top of the list.
Try this:
trim(get_film_info('main description'));
Your text has space at beginning!
I don't know what function 'get_film_info' returns but it returns with space!
There's definitely a space in the beginning and one at the end, as can be seen in the page source. Perhaps get_film_info() is inadvertently injecting them.
There is a heading space in the return value of get_form_info(). Check the value of 'main description' in your database (or whereever it is being stored). If there isn't a heading space in the value itself, then get_film_inflo() is to blame.
A space does exist. Outside of the textarea, the browser does not interpret them, because a \n means nothing (it is interpreted in the source code only) in just regular text form. However, a \n inside a textarea represents a line break and is being interpreted as such.
To solve the problem you can always trim the value before outputting it.

Categories