简体   繁体   中英

How can I indent text output to the command line with PHP?

I'm displaying text from a php process that's running on the command line on linux. I'd like to format some free text data so that it is indented on screen when it's outputted. Sometimes it's a long field and will wrap the screen no matter how big the screen is.

Is there a good way to force the command line output to indent the wrapping or a way to easily format the text in php and maybe pre-format it so it displays cleanly, wrapped, and indented on screen?

I realize I can write something to do this, but was wondering if there's any standard command feature built into linux.

PHP's built in function, wordwrap() is a simple and great solution for what I needed.

$SomeText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer rutrum neque euismod ante pulvinar gravida. In viverra sollicitudin libero nec ullamcorper. Nam porta luctus leo, vel porttitor enim rutrum id. Fusce lacus odio, facilisis eget ligula et, consequat faucibus turpis. Vestibulum sed placerat leo, eu rhoncus leo."

Wraping Problem Example

echo "\n\n -- SomeText: $SomeText";

Outputs...

 -- SomeText: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer rutrum 
neque euismod ante pulvinar gravida. In viverra sollicitudin libero nec ullamcorper. 
Nam porta luctus leo, vel porttitor enim rutrum id. Fusce lacus odio, facilisis eget 
ligula et, consequat faucibus turpis. Vestibulum sed placerat leo, eu rhoncus leo.

Success with wordwrap()

echp "\n\n -- SomeText: ".wordwrap($SomeText,100,"\n    ");

Outputs...

 -- SomeText: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    Integer rutrum neque euismod ante pulvinar gravida. In viverra
    sollicitudin libero nec ullamcorper. Nam porta luctus leo, vel
    porttitor enim rutrum id. Fusce lacus odio, facilisis eget ligula et,
    consequat faucibus turpis. Vestibulum sed placerat leo, eu rhoncus leo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM