/    Sign up×
Community /Pin to ProfileBookmark

Problem with substr

Hi, everyone Im trying to create something similar to

[URL=”http://www.thefirstpost.co.uk/?storyID=24048″]http://www.thefirstpost.co.uk/?storyID=24048[/URL]

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:

[code=php]<?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>”;

?>[/code]

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

[URL=”http://www.emeritus-consultants.co.uk/clickliverpool/admin2/test.php”]Test[/URL]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 20.2008 — I'd probably use the wordwrap() function to do the splitting, in order to avoid breaking in the middle of a word.
[code=php]
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]);
[/code]

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.
Copy linkTweet thisAlerts:
@cs3mwauthorMar 21.2008 — 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!
Copy linkTweet thisAlerts:
@NogDogMar 22.2008 — 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![/QUOTE]
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.
Copy linkTweet thisAlerts:
@cs3mwauthorMar 22.2008 — 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.
Copy linkTweet thisAlerts:
@NogDogMar 22.2008 — 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 "rn" if it comes from a Windows source.
Copy linkTweet thisAlerts:
@cs3mwauthorMar 22.2008 — yeah that does work although its not the desired effect I want. AAAAhh!! Thanks anyway!

Mike
×

Success!

Help @cs3mw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...