note 48836 added to ref.strings

From: Date: Sat, 08 Jan 2005 16:15:52 +0000
Subject: note 48836 added to ref.strings
Groups: php.notes 
Request: Send a blank email to php-notes+get-82956@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/ref.strings.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+48836 Delete -- http://master.php.net/manage/user-notes.php?action=delete+48836&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+48836&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+48836&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+48836&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+48836&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+48836&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+48836&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#82956) next »