Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > BOOK: Beginning PHP 5.3
|
BOOK: Beginning PHP 5.3
This is the forum to discuss the Wrox book Beginning PHP 5.3 by Matt Doyle; ISBN: 978-0-470-41396-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 5.3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 16th, 2011, 07:55 PM
Authorized User
 
Join Date: Jun 2011
Posts: 51
Thanks: 9
Thanked 0 Times in 0 Posts
Send a message via MSN to nawar youssef
Default Working with Strings Function Try It Out page : 83

I wrote this code and I checked it many times but it does not work all what I have on the screen is the ( Justifying Lines of Text ) text!!!!!! which is the first html code.
anybody know what is the problem, two people always better than one.
thanks.

<?php

// The text to justify

$myText = <<<END_TEXT
But think not that this famous town has
only harpooneers, cannibals, and
bumpkins to show her visitors. Not at
all. Still New Bedford is a queer place.
Had it not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.

END_TEXT;

$myText = str_replace ("\r\n", "\n", $myText);
$lineLength = 40;
$myTextJustfied = " ";
$numLines = substr_count($myText, "\n");
$startOfLine = 0;

for ($i= 0; $i< $numLines; $i++){

$orejLineLength = strpos($myText, "\n", $startOfLine) - $startOfLine;
$justfiedLine = substr($myText, $startOfLine, $orejLineLength);
$justFiedLineLength = $orejLineLength;

while ( $i < $numLines -1 && $justFiedLineLength < $lineLength ) {

for ( $j =0; $j <$justFiedLineLength; $j++) {

if ( $justFiedLineLength < $lineLength && $justfiedLine[$j] == " " ) {

$justfiedLine = substr_replace ( $justfiedLine, " ", $j, 0);
$justfiedLineLength++;
$j++;
}
}
}

$myTextJustfied .= "$justfiedLine\n";
$startOfLine += $orejLineLength +1;
}
?>

<h2>Original text:</h2>
<pre><?php echo $myText ?></pre>

<h2>Justified text:</h2>
<pre><?php echo $myTextJustfied ?></pre>

</body>
</html>
 
Old September 11th, 2011, 09:29 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Easy mistake to make... HARD to find.

In your code
Code:
$justFiedLineLength = $orejLineLength;

while ( $i < $numLines -1 && $justFiedLineLength < $lineLength ) {

for ( $j =0; $j <$justFiedLineLength; $j++) {

if ( $justFiedLineLength < $lineLength && $justfiedLine[$j] == " " ) {

$justfiedLine = substr_replace ( $justfiedLine, " ", $j, 0);
$justfiedLineLength++;
$justFiedLineLength has a small F in THe Last line of this snippit ...
$justfiedLineLength++;
The Following User Says Thank You to GeneBuchite For This Useful Post:
nawar youssef (September 12th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
BIND attribute (page 83) Stephan_GER BOOK: Professional ASP.NET MVC 2 2 September 7th, 2010 12:40 PM
concat function for strings franta1346 Reporting Services 0 May 30th, 2007 06:47 AM
Passing Form Strings through on the same page perryml Classic ASP Basics 0 November 22nd, 2006 12:58 PM
Working with HEX'ed Binary Registry Strings sencee C# 2 April 13th, 2006 03:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.