/    Sign up×
Community /Pin to ProfileBookmark

Counting number of text lines

Hi,
I’m thinking there isn’t a sensible solution to this but you guys might have some ideas.

I’ve made a page where PHP dynamically puts text content on a page by loading it into a string from a text file of projects. It’s given a certain font size and width to fit into using CSS. For some projects, the text is quite short and fits in fine. For others, it overflows the space allowed.

Apart from working out the average number of characters that causes it to spill over, is there some way I could get PHP (or CSS?) to say “fit the text into this space – if the auto word-wrapping creates >= X lines, generate a ‘page 2’ button”?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Jul 22.2009 — You might want to look at a javascript or css solution to that though css won't be able to add links by itself. Using javascript is probably your best bet since it will have the best access to the particulars of the rendering method and engine of the browser each user is using.
Copy linkTweet thisAlerts:
@MindzaiJul 22.2009 — I'd disagree, this is perfectly possible, and probably easier, using PHP.
Copy linkTweet thisAlerts:
@derekjacksonauthorJul 23.2009 — I'd disagree, this is perfectly possible, and probably easier, using PHP.[/QUOTE]

Good to hear - any ideas how?! ?
Copy linkTweet thisAlerts:
@MindzaiJul 23.2009 — Firstly I would recommend against having a fixed width and fixed height element to fit your text into. Unless you are using a monospaced font you will never know if a certain block of text is going to fit. I would take the approach of fixing the width if necessary, but allowing some flexibility in the height of the block. You can then decide on a maximum number of characters to show on each page and display chunks.

This is a very basic example but it should give you an idea of one way to go about things:

[code=php]
<?php

$string = "This is a test string whichnI will attempt to splitn into chunks and display";

function string_chunks($string, $chunk_size = 500) {
if (strlen($string) <= $chunk_size) {
return $string;
}
$string = str_replace("n", "n ", $string);
$words = array_chunk(preg_split('/ +/', $string), $chunk_size);
return array_map('implode', $words, array_fill(0, sizeof($words), ' '));
}

$pages = string_chunks($string, 4);
$num_pages = sizeof($pages);

if (!$num_pages) {
echo "<p>No text to display!</p>";
} elseif (isset($_GET['page']) && array_key_exists($_GET['page'] - 1, $pages)) {
echo $pages[$_GET['page'] - 1];
} else {
echo $pages[0];
}

?>

<ul class="pagination">
<?php for ($i=1; $i<=$num_pages; $i++): ?>
<li><a href="<?php echo $_SERVER['PHP_SELF'] ?>?page=<?php echo $i ?>">Page <?php echo $i ?></a></li>
<?php endfor ?>
</ul>[/code]
×

Success!

Help @derekjackson 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.27,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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