Jump to content

limit words

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
hey guys i have seen this before but have no clue how to do it.

when you insert texte with a titles in a database.when you extract it and put just the titke on a page with very limited space if the title is to long it deforms the page.

i have seen like

tutorial on how.....

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What kind of a limit do you have in mind?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
just like a limit of 10 caracters in the box

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can use the maxlength property to limit text input of an input field.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
what about in an echo?

cause on my page i have an echo(title)

i think the notification on the forums dosnt work all the time..

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I would do something like this:

function smarttopic($topic, $max=30, $min=0) {
    if ($min == 0) {
        $min = $max-10;
    }
    if (strlen($topic) < $max) {   
        return $topic;  //if string isn't too long, return the whole string
    } else {
        $part = substr($topic, $min, $max);
        $pos = strpos($part, " ");
        if ($pos) { // if there is a space between min and max, cut at it
           return substr($topic, 0, $min+$pos)."...";
        else { // otherwise, cut at mid between min and max
           return substr($topic, 0, $min(+($max-$min)/2))."...";
        }
    }
}

usage:
echo smarttopic("this is a long but spaced topic that will have a good cut", 30, 20);
echoes
"this is a long but spaced..."

echo smarttopic("long word topics like intercontinental airlines"), 40);
echoes
"long word topics like intercontinen..."

Edited by Orjan, 02 March 2009 - 03:54 AM.
first code line if had an = too little.

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
techker

techker

    Programmer

  • Members
  • PipPipPipPip
  • 136 posts
thx i will try this out!

#8
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Great work Orjan!
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
I noticed an error, there was an equal sign (=) lacking on the first row (at the if). edited.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall