1

I am using Javascript to hide / show a blog-post stored in a mysql table. The script for doing this is:

<script type="text/javascript">
    function unhide(divID) {
        var item = document.getElementById(divID);
        if (item) {
            item.className = (item.className == 'hidden') ? 'unhidden' : 'hidden';
        }
    }
</script>

This links to some css styling:

.hidden {
 display: none;}
.unhidden {
 display: inline;}

I am calling the script via a href styles as a button:

<a class=button href="javascript:unhide('first_post');">More</a>

As for the content I originally tried the following to initially show a small section of text, then the rest after the link is clicked:

<?php $var = mysql_result($result,0,"post_text"); ?>

<?php echo substr($var, 0, 400); ?>
<div id="first_post" class = "hidden">
  <?php echo substr($var, 400, 5000)?>
</div>

However where the two sets of sub-strings join there is a space. For example if the first sub-string ends in "the tree's hav" and the second sub-string starts "e eyes you know" the concatenation results in "the trees hav e eyes you know"

Can anyone help me with this problem?

Colin Brock
  • 21,267
  • 9
  • 46
  • 61

2 Answers2

2

Remove newlines between <?php ?> and <div> tags - this should help you get rid of spaces.

<?php echo substr($var, 0, 400); ?><div id="first_post" class = "hidden"><?php echo substr($var, 400, 5000)?></div>
bjauy
  • 929
  • 16
  • 22
1

I think what you're looking for is you want to truncate string from the end of the word rather than giving link somewhere in between. That's what I see as permanent solution...

When I googled up expecting that PHP would have something available out of the box found following 2 article which might help you.

http://css-tricks.com/snippets/php/truncate-string-by-words/

How to Truncate a string in PHP to the word closest to a certain number of characters?

They are not exactly what you're looking for but they can be of great help if you take inspiration from the concepts.

Community
  • 1
  • 1
deej
  • 2,536
  • 4
  • 29
  • 51