note 48836 deleted from ref.strings by derick

From: Date: Wed, 09 Feb 2005 18:09:57 +0000
Subject: note 48836 deleted from ref.strings by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-84736@lists.php.net to get a copy of this message
Note Submitter: Vip Malixi ---- I always thought that reverse-indenting text (first line in a paragraph is indented to the left by a few characters) without using tables was impossible, or, even if you do use tables, you won't be able to flow the text to the next lines. I should have realized that with PHP, nothing's impossible. So here it is, how to reverse indent text without using tables (pretty useful for continuous text you want to reverse indent, and for links you want to reverse indent) and also allow continuous flow of text from the first line onwards: /* Note: if you're going to use my function, please retain my name, that's all I ask. Thanks. This function indents by 4 spaces. You can add or reduce by adding or deleting "&nbsp;". */ // $max_line_width is in number of characters function vip_reverse_indent($str, $max_line_width) { $str2 = explode(" ", $str); $line_width = 0; for ($c = 0; $c < count($str2); $c++) { echo $str2[$c] . " "; $line_width = $line_width + (strlen($str2[$c]) + 1); if ( ($c + 1) < count($str2) ) { $test_width = $line_width + (strlen($str2[$c + 1]) + 1); if ($test_width > $max_line_width) { echo "<BR>&nbsp;&nbsp;&nbsp;&nbsp;"; $line_width = 4; } //endif } //endif } //endfor }//end function vip_reverse_indent() sample use: #1 reverse indents text by 25 chars: vip_reverse_indent("I sit with both legs outstretched, free of the true, free of the false. Sitting quietly, doing nothing, spring comes and the grass grows by itself. I eat, sleep, drink, get up. All I have to do after that is to die.", 25); or #2 reverse indents text by 35 chars: $anystr = "I sit with both legs outstretched, free of the true, free of the false. Sitting quietly, doing nothing, spring comes and the grass grows by itself. I eat, sleep, drink, get up. All I have to do after that is to die."; vip_reverse_indent($anystr, 35); output #1: I sit with both legs outstretched, free of the true, free of the false. Sitting quietly, doing nothing, spring comes and the grass grows by itself. I eat, sleep, drink, get up. All I have to do after that is to die. #2: I sit with both legs outstretched, free of the true, free of the false. Sitting quietly, doing nothing, spring comes and the grass grows by itself. I eat, sleep, drink, get up. All I have to do after that is to die.

« previous php.notes (#84736) next »