php causing me 'HEADACHES!!!!! plz help!

Do not post support requests, bug reports or feature requests. Discuss phpBB here. Non-phpBB related discussion goes in General Discussion!
Scam Warning
Locked
Paul needs help plz!
Registered User
Posts: 1
Joined: Wed Aug 07, 2013 1:39 pm

php causing me 'HEADACHES!!!!! plz help!

Post by Paul needs help plz! » Wed Aug 07, 2013 2:24 pm

I have been reading a php book titled: 'beginning php 5.3,, by matt doyle' and I am stumped by a piece of code he uses as part of a script - to justify lines of text.

The code is below. Basically, what this code does is take some text and justifies the text
aligning the text within a column so that the text is flush with both the left and right margins.

The line of code which I cannot understand is:

Code: Select all

$originalLineLength = strpos( $myText, “\n”, $startOfLine ) - $startOfLine;
...... to be more specific -------> - $startOfLine; <-------- i do not understand why this piece of code is needed or what it actually does!!


Can someone please HELP!!


PS,

I don't have any visuals to show you but I have a link to the author's book (which contains visuals ( which you can view online for free),,,, page 82. [URL removed by moderator]


Paul.


Code: Select all

$numLines = substr_count( $myText, “\n” );
$startOfLine = 0;
// Move through each line in turn
for ( $i=0; $i < $numLines; $i++ ) {
$originalLineLength = strpos( $myText, “\n”, $startOfLine ) - $startOfLine;
$justifiedLine = substr( $myText, $startOfLine, $originalLineLength );
$justifiedLineLength = $originalLineLength;
// Keep adding spaces between words until the desired
// line length is reached
while ( $i < $numLines - 1 & & $justifiedLineLength < $lineLength ) {
for ( $j=0; $j < $justifiedLineLength; $j++ ) {
if ( $justifiedLineLength < $lineLength & & $justifiedLine[$j] == “ “ ) {
$justifiedLine = substr_replace( $justifiedLine, “ “, $j, 0 );
$justifiedLineLength++;
$j++;
}
}
}
// Add the justified line to the string and move to the
// start of the next line
$myTextJustified .= “$justifiedLine\n”;
$startOfLine += $originalLineLength + 1;
}
?>
< h2 > Original text: < /h2 >
< pre > <?php echo $myText ?> < /pre >
< h2 > Justified text: < /h2 >
< pre > <?php echo $myTextJustified ?>< /pre >
$numLines = substr_count( $myText, “\n” );
$startOfLine = 0;
// Move through each line in turn
for ( $i=0; $i < $numLines; $i++ ) {
$originalLineLength = strpos( $myText, “\n”, $startOfLine ) - $startOfLine;
$justifiedLine = substr( $myText, $startOfLine, $originalLineLength );
$justifiedLineLength = $originalLineLength;
// Keep adding spaces between words until the desired
// line length is reached
while ( $i < $numLines - 1 & & $justifiedLineLength < $lineLength ) {
for ( $j=0; $j < $justifiedLineLength; $j++ ) {
if ( $justifiedLineLength < $lineLength & & $justifiedLine[$j] == “ “ ) {
$justifiedLine = substr_replace( $justifiedLine, “ “, $j, 0 );
$justifiedLineLength++;
$j++;
}
}
}
// Add the justified line to the string and move to the
// start of the next line
$myTextJustified .= “$justifiedLine\n”;
$startOfLine += $originalLineLength + 1;
}
?>
< h2 > Original text: < /h2 >
< pre > <?php echo $myText ?> < /pre >
< h2 > Justified text: < /h2 >
< pre > <?php echo $myTextJustified ?>< /pre >
Last edited by Tom on Wed Aug 07, 2013 2:32 pm, edited 1 time in total.
Reason: Removed URL to PDF of book (see Rule 1e: https://www.phpbb.com/rules/#warez). Also added [code] tags for easier legibility.

User avatar
Tom
Former Team Member
Posts: 2665
Joined: Tue Jun 20, 2006 2:12 am
Name: Tom Catullo
Contact:

Re: php causing me 'HEADACHES!!!!! plz help!

Post by Tom » Wed Aug 07, 2013 2:39 pm

Hello,

The community forums here at phpBB.com are not intended to provide support for general PHP programming inquiries, unless they are directly related to modifying or working with the phpBB software. Your inquiry has no relation to the phpBB software, and therefore unfortunately has no place here on the phpBB.com community forums. There are plenty of other forums on the web that offer PHP programming support, however, so I suggest you search online for one of those forums to seek for help instead.

Also, I removed the link you provided to the "free" PDF version of the book you mentioned, as it is indeed copyrighted and the site on which it is hosted shows no signs that they have the rights to be providing that content freely to the public. Please see Rule 1e on the posting of copyrighted material on our forums for more information on that.

I will be closing this topic now. Please contact me via PM if you have any further questions regarding why your topic was closed. Thanks.
Tom Catullo - Former Moderator Team Member
phpBB3 Smiley Pak Generator | Legend Repositioning MOD | My GitHub | My Site

Locked

Return to “phpBB Discussion”