Wrapping text in table cells and DIV tags

I’ve been doing research on how to wrap texts. First solution I found was giving the CSS property word-wrap the value break-word:

word-wrap: break-word

But this didn’t work in Firefox.

In some forum, some suggested the use of overflow-x:auto:

<div style=”width:80px; overflow-x:auto;”>
Pneumonoultramicroscopicsilicovolcanoconiosis
</div>

But it doesn’t look good, does it?

Then I came across this blog entry. The author used the following PHP code to add a zero-width character to every character that is not a number or letter:


<?php
$url = preg_replace('/([^a-zA-Z0-9])/', "$1​", $url);
?>

It worked for me after tweaking the code a bit. I removed the tilde (^) sign:


<?php
$url = preg_replace('/([a-zA-Z0-9])/', "$1​", $url);
?>

With this, the zero-width character is inserted after every letter and number, allowing a long text wrap in its container.

Example:

<?php
$longtext = "Pneumonoultramicroscopicsilicovolcanoconiosis";
$longtext = preg_replace('/([a-zA-Z0-9])/', "$1​", $longtext);
?>

<div style="width:100px;background:#cccccc;">
<?php echo $longtext ?>
</div>

It’s all thanks to the guy who posted this blog entry. 🙂

Edit: I found out that the wrapping using PHP helps only for the display, but not when you need to copy the displayed text and paste it somewhere like notepad. The zero-width characters would appear in between every character displayed.

Related Posts:

Posts that may be related to "Wrapping text in table cells and DIV tags":

Catzie

A Filipino programmer with a variety of interests such as baking, singing, making up silly song/rap lyrics, K-pop, drawing, creating unique dessert flavors, obsessing about finding out how some things works, board games, anime, video games, and forgetting things that usually go in her long list of interests. Running small-time online dessert shops Cookies PH and Catzie's Cakery.

Leave a Reply

Your email address will not be published. Required fields are marked *