Limiting HTML char count code with php.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sims

    Limiting HTML char count code with php.

    Hi,

    I know how to limit the number of characters using substr,
    (http://uk.php.net/manual/en/function.substr.php).
    But when using html that is not going to work because of all the possible
    tags, (anchor, image, table...etc).

    for example, if I have some text...
    $text = "<a href='somelonnn nnnngg'>this is a small text</a>";

    I want to create a function to make sure that there is not more than 10
    chars displayed.

    but if I use

    $text1 = substr($text, 0,10 );
    i get $text1= "<a href='s";

    where I would like to get

    $text = "<a href='somelonnn nnnngg'>this is a </a>";
    ^^ only 10
    chars.

    How could the above be achieved?

    Many thanks.

    Sims




  • Five Cats

    #2
    Re: Limiting HTML char count code with php.

    In message <c5j0ec$2834l$1 @ID-162430.news.uni-berlin.de>, Sims
    <siminfrance@ho tmail.com> writes[color=blue]
    >Hi,
    >
    >I know how to limit the number of characters using substr,
    >(http://uk.php.net/manual/en/function.substr.php).
    >But when using html that is not going to work because of all the possible
    >tags, (anchor, image, table...etc).
    >
    >for example, if I have some text...
    >$text = "<a href='somelonnn nnnngg'>this is a small text</a>";
    >
    >I want to create a function to make sure that there is not more than 10
    >chars displayed.
    >
    >but if I use
    >
    >$text1 = substr($text, 0,10 );
    >i get $text1= "<a href='s";
    >
    >where I would like to get
    >
    >$text = "<a href='somelonnn nnnngg'>this is a </a>";
    > ^^ only 10
    >chars.
    >
    >How could the above be achieved?[/color]

    In essence: Pick out the bit between the single quotes and check it's
    length.

    All the stuff you need is at:


    (see explode, strlen)

    BTW what are you planning doing it it's too long?



    --
    Five Cats
    Email to: cats_spam at uk2 dot net

    Comment

    • Sims

      #3
      Re: Limiting HTML char count code with php.

      > >[color=blue][color=green]
      > >How could the above be achieved?[/color]
      >
      > In essence: Pick out the bit between the single quotes and check it's
      > length.[/color]

      Sorry I am not sure I follow what you are saying.
      I want the output text length to be limited not the tags.
      [color=blue]
      >
      > All the stuff you need is at:
      >
      > http://www.php.net/manual/en/ref.strings.php
      > (see explode, strlen)
      >
      > BTW what are you planning doing it it's too long?[/color]

      Well if I can limit the number of characters it is easier to display.
      for example if I do not want a table to have more than 50 chars then I could
      truncate them, but I only want to truncate characters that will be
      displayed.
      not the html tags that are not displayed.

      In my example given I want to limit to 10 the number of characters
      displayed,
      but not the tags should be excluded.

      $text = "<a href='somelonnn nnnngg'>this is a </a>";
      ^^ only 10
      chars
      [color=blue]
      >
      > --
      > Five Cats[/color]

      Sims



      Comment

      • Kevin Thorpe

        #4
        Re: Limiting HTML char count code with php.

        Sims wrote:[color=blue]
        > Hi,
        >
        > I know how to limit the number of characters using substr,
        > (http://uk.php.net/manual/en/function.substr.php).
        > But when using html that is not going to work because of all the possible
        > tags, (anchor, image, table...etc).
        >
        > for example, if I have some text...
        > $text = "<a href='somelonnn nnnngg'>this is a small text</a>";
        >
        > I want to create a function to make sure that there is not more than 10
        > chars displayed.
        >
        > but if I use
        >
        > $text1 = substr($text, 0,10 );
        > i get $text1= "<a href='s";
        >
        > where I would like to get
        >
        > $text = "<a href='somelonnn nnnngg'>this is a </a>";
        > ^^ only 10
        > chars.
        >
        > How could the above be achieved?[/color]

        As the quote goes.... 'I wouldn't go there from here'.

        Why are you parsing the finished href? Can't you trim the text before
        building the href?

        Alternatively use regular expressions to parse out the bit between the tags.

        Comment

        • Garp

          #5
          Re: Limiting HTML char count code with php.


          "Kevin Thorpe" <kevin@pricetra k.com> wrote in message
          news:407d243b$0 $23747$afc38c87 @news.easynet.c o.uk...[color=blue]
          > Sims wrote:[color=green]
          > > Hi,
          > >
          > > I know how to limit the number of characters using substr,
          > > (http://uk.php.net/manual/en/function.substr.php).
          > > But when using html that is not going to work because of all the[/color][/color]
          possible[color=blue][color=green]
          > > tags, (anchor, image, table...etc).
          > >
          > > for example, if I have some text...
          > > $text = "<a href='somelonnn nnnngg'>this is a small text</a>";
          > >
          > > I want to create a function to make sure that there is not more than 10
          > > chars displayed.
          > >
          > > but if I use
          > >
          > > $text1 = substr($text, 0,10 );
          > > i get $text1= "<a href='s";
          > >
          > > where I would like to get
          > >
          > > $text = "<a href='somelonnn nnnngg'>this is a </a>";
          > > ^^ only 10
          > > chars.
          > >
          > > How could the above be achieved?[/color]
          >
          > As the quote goes.... 'I wouldn't go there from here'.
          >
          > Why are you parsing the finished href? Can't you trim the text before
          > building the href?
          >
          > Alternatively use regular expressions to parse out the bit between the[/color]
          tags.

          Or, use strip_tags().

          Garp


          Comment

          • John Dunlop

            #6
            Re: Limiting HTML char count code with php.

            Garp wrote:
            [color=blue]
            > Or, use strip_tags().[/color]

            That function name is a misnomer; it ought to be renamed:
            strip_everythin g_between_lesst han_and_greater than.

            :-/ I'm one third serious.

            --
            Jock

            Comment

            Working...