LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2017, 12:59 PM   #1
shams
Member
 
Registered: Jan 2004
Posts: 453

Rep: Reputation: 30
how to ouput text in lines in php?


Hi,
In this php code the file_get_contents(), gets the text and passing to write on image the problem is, file_get_contents() passing all the text in one line not breaks to each new line, so all the text in image is in one line, how i can break to each new line:
PHP Code:
<?php

require __DIR__.'/../vendor/autoload.php';

use 
GDText\Box;
use 
GDText\Color;

include(
'../src/FarsiGD.php');
$im imagecreatetruecolor(500500);
$backgroundColor imagecolorallocate($im01864);
imagefill($im00$backgroundColor);

$box = new Box($im);
$box->setFontFace(__DIR__.'/fonts/BahijRegular.ttf');
$box->setFontColor(new Color(25575140));
$box->setTextShadow(new Color(00050), 22);
$box->setFontSize(23);
$box->setLineHeight(1.5);
$box->enableDebug();
$box->setBox(2020460460);
$box->setTextAlign('right''top');

$gd = new FarsiGD();

$text file_get_contents('/home/user/message');

$text $gd->persianText($text'fa''normal');
 
$box->draw($text);

header("Content-type: image/png;");
imagepng($imnull9PNG_ALL_FILTERS);

Last edited by shams; 07-31-2017 at 01:55 PM.
 
Old 07-31-2017, 04:16 PM   #2
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 386

Rep: Reputation: 128Reputation: 128
It looks like that's what file_get_contents does..."Reads entire file into a string"
Try file, which will load each line into an array element, then loop the array to populate $text, appending a line feed at the end of each element.
Or fget, to retrieve one line at a time and add to $text, again with appended line feed.

Alternate (kinda geeky) idea: Put some "special character" (something like xxx) at the end of each line, then substitute \n for xxx in $text before doing the draw function.

Last edited by scasey; 07-31-2017 at 04:18 PM.
 
Old 08-01-2017, 09:39 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,629
Blog Entries: 4

Rep: Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999
Quote:
Originally Posted by scasey View Post
It looks like that's what file_get_contents does..."Reads entire file into a string"
Try file, which will load each line into an array element, then loop the array to populate $text, appending a line feed at the end of each element.

Or fget, to retrieve one line at a time and add to $text, again with appended line feed.
I'm guessing that you are outputting to a web page. In this case, "line feed" would not be relevant: you would use the <br/> tag.

One simple way to do it is to split() the string into an array, then immediately join() that array using '<br/>' as the separator. This will reassemble the string with a break separating each piece. Output the resulting string followed by another break. That's a convenient way to do it if memory is not an issue.

If it might be, you can read the file "line by line" and output each line followed by a break. An arbitrarily-large file can be output in this way.

Last edited by sundialsvcs; 08-01-2017 at 09:41 AM.
 
Old 08-01-2017, 11:59 AM   #4
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 386

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by sundialsvcs View Post
I'm guessing that you are outputting to a web page. In this case, "line feed" would not be relevant: you would use the <br/> tag.
Perhaps...although the posted script is using the data in $text to create an image (.png) file. In any case, that's why I used "line feed" instead of \n or CRLF or <br>, because I didn't know what was specifically appropriate.
Again, using php's file function will load an array as you suggest, and yes, using php's join() function could be used to add the "line feed"
 
Old 08-02-2017, 09:00 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 8,629
Blog Entries: 4

Rep: Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999Reputation: 2999
If you are generating an image and outputting that image, then there are PHP functions to do that, and it is imperative that your program produces no other output. (For instance, a single blank space following a ?> tag at the end of the file, which will corrupt the image output by adding a space-valued byte at the end.)
 
Old 08-03-2017, 12:11 AM   #6
shams
Member
 
Registered: Jan 2004
Posts: 453

Original Poster
Rep: Reputation: 30
Thaks for replies, i got this piece of code, this code prints the text in new lines:
PHP Code:
<?php
$filestring 
file_get_contents('/home/user/message');
    
$filearray explode("\n"$filestring);
while (list (
$key$val) = each ($filearray) ) print "$val<br />";
?>
Now i am trying to add this code to my one, what it do is this:
PHP Code:
$gd = new FarsiGD();

$filestring file_get_contents('/home/user/message');
    
$filearray explode("\n"$filestring);
while (list (
$key$val) = each ($filearray) );


$val $gd->persianText($val'fa''normal');

$box->draw($val); 
But when run the code get only blank image without text, means code doesn't get the text.
 
Old 08-03-2017, 11:37 AM   #7
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,227

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
maybe you are looking for fgets?

Code:

if ($file = fopen("file.txt", "r")) {
    while(!feof($file)) {
        $line = fgets($file);
        # do same stuff with the $line
    }
    fclose($file);
}
 
1 members found this post helpful.
Old 08-04-2017, 01:55 AM   #8
shams
Member
 
Registered: Jan 2004
Posts: 453

Original Poster
Rep: Reputation: 30
how to ouput text in new lines?

Thanks for reply, i run the code it print the whole text in one line, can you please help me how to output the text in new lines as it's original text.

Last edited by shams; 08-04-2017 at 02:00 AM.
 
Old 08-04-2017, 02:05 AM   #9
scasey
Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 386

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by shams View Post
Thanks for reply, i run the code it print the whole text in one line, can you please help me how to output the text in new lines as it's original text.
Please read reply #2...and #3 and #4

In short, don't use file_get_contents(), use file() or fget()

Last edited by scasey; 08-04-2017 at 02:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] diff: No line numbers in output of two files' difference andrew.comly Programming 1 07-08-2016 04:20 AM
Redirect only numbers in command line output sagar666 Linux - Server 2 04-15-2015 03:57 AM
'line numbers' option in [code] tags nadroj LQ Suggestions & Feedback 6 04-15-2007 06:39 PM
Grep's line numbers parsed into one line of output. judgex Programming 8 08-14-2006 05:22 AM
printing line numbers for code bauer172uw Programming 3 04-14-2006 12:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration