Click to See Complete Forum and Search --> : Problem with substr


cs3mw
03-20-2008, 11:44 AM
Hi, everyone Im trying to create something similar to

http://www.thefirstpost.co.uk/?storyID=24048

where the news article will be extracted from the database and the half the content is stored in one div and the other half in another. Now Ive created the following test:


<?php


$sql="select article from article WHERE articleID='189'";
$result=mysql_query($sql) or die ("Error!! BAD SELECT SEARCH STATEMENT!!");
$nrows = mysql_num_rows($result);
$row = mysql_fetch_array($result);
extract($row);


$stringlength = strlen($article);


$remainder = $stringlength % 2;


if ($remainder > 0) {
$p = $stringlength / 2;
$p = $p + 0.5;
echo"<br/><br/>$p";
}

else {
$p = $stringlength /2;
}


$par1 = substr($article, 0, $p);

echo"<br/><br/><div style='width:300px;border:1px solid black;align:left'><p align='justify'>$par1</p></div>";



$par2 = substr($article, $p);

echo"<br/><br/><div style='width:300px;border:1px solid black;align:justify'>$par2</div>";

?>

Now please excuse the above code it is just an example il change the variables etc later. Now my problem is Im wondering if there is a way the first paragraph actually finishes with a word pushed right up to the right alignment in the way the First Post does. At the moment the split occurs and there is a rather large space between the last word in the paragraph and the right border of the div. Hope this makes sense hopefully the following link will help you out. This is how its displayed

Test (http://www.emeritus-consultants.co.uk/clickliverpool/admin2/test.php)

NogDog
03-20-2008, 05:02 PM
I'd probably use the wordwrap() function to do the splitting, in order to avoid breaking in the middle of a word.

function column_split($text, $cols=2)
{
$delim = '<[{break}]>';
$colLength = ceil( strlen($text) / $cols );
$text = wordwrap($text, $colLength, $delim);
return explode($delim, $text, $cols);
}

// usage:
$columns = column_split($someText, 2);
$column1 = trim($columns[0]);
$column2 = trim($columns[1]);

You'll still need to flesh this out to deal with how you want to handle splits in the middle of a paragraph, etc.; but that will depend on how the text is already formatted/marked-up before the splitting is done.

cs3mw
03-21-2008, 03:34 PM
Yeah I have got the function to avoid splitting a word in half using strpos. However my problem is I need the first paragraph to have no space between the last word and the right edge of the div. Thanks for your input!

NogDog
03-21-2008, 07:43 PM
Yeah I have got the function to avoid splitting a word in half using strpos. However my problem is I need the first paragraph to have no space between the last word and the right edge of the div. Thanks for your input!
Anything you might do is dependent on the font family and size, and so depends on the user's browser (a) supporting that font and (b) not having a browser setting or personal stylesheet setting that overrides your desired setting. For instance, go to the site you referenced and change the default font size on your browser: suddenly things don't line up as nicely. I suspect the creator of that site just manually tried different break points until he found one that worked nicely with the page's default style settings.

cs3mw
03-22-2008, 03:03 PM
ah right so really there not as smart as they make out ha!!! just an idea though is there a way you can recognise a line break in a group of text because Im sure I could do something with that.

NogDog
03-22-2008, 05:56 PM
Depends on how the text you're working with is formatted, but normally you can look for the (doubl-quoted) string "\n", though it may be "\r\n" if it comes from a Windows source.

cs3mw
03-22-2008, 06:25 PM
yeah that does work although its not the desired effect I want. AAAAhh!! Thanks anyway!

Mike