Professional Web Applications Themes

Cutting strings short with php - PHP Development

Hi All, Here's my problem. I have a large textual entry in a database which I want to extract and display on a webpage using php. Everything's ok, so far. However, I only want to show the first 100 characters of the text, not all of it. The problem is that the text includes HTML tags. If I simply cut the text off at 100 characters, there's a chance that I will do so in the middle of a HTML tag which will then break the rest of the page. So my question is: can I use php to help ...

  1. #1

    Default Cutting strings short with php

    Hi All,
    Here's my problem. I have a large textual entry in a database which I
    want to extract and display on a webpage using php. Everything's ok, so far.
    However, I only want to show the first 100 characters of the text, not all
    of it. The problem is that the text includes HTML tags. If I simply cut the
    text off at 100 characters, there's a chance that I will do so in the middle
    of a HTML tag which will then break the rest of the page.

    So my question is: can I use php to help me detect an appropriate place to
    cut the string?. Is there a neat function to do this or will I have to muck
    around with string ysis functions?. Alternatively is there a way to
    render the whole string but only make the first 100 characters visible?.
    Maybe this would be neater?.

    Any comments or suggestions gratefully received.

    Ta,
    Dave




    Dave Guest

  2. #2

    Default Re: Cutting strings short with php

    Dave Moore wrote: 
    you could use regular expressions to split the string into text and tags.

    Regards
    Stefan
    Stefan Guest

  3. #3

    Default Re: Cutting strings short with php

    There is a function in PHP that strips the HTML tags called "strip_tags"
    then when you get the string back with no tags use "substr" or something to
    get the first 100 chars.


    "Dave Moore" <freeserve.co.uk> wrote in message
    news:dbjluv$chk$svr.pol.co.uk... 


    Hello Guest

  4. #4

    Default Re: Cutting strings short with php

    Hello wrote: 
    How will that help him, since I guess he needs the HTML tags?!

    Regards
    Stefan
     
    Stefan Guest

  5. #5

    Default Re: Cutting strings short with php

    Stefan Rybacki wrote: 
    > How will that help him, since I guess he needs the HTML tags?!
    >[/ref]

    I could see that possibly causing some problems, since you would have
    opening tags but no closing tags. Depending on the tags, of course:

    <pre>This is a string of more than 100 characters. This is a string of more
    than 100 characters. This is a string of more than 100 characters. This is a
    string of more than 100 characters. This is a string of more than 100
    characters. This is a string of more than 100 characters.</pre>

    could cause the rest of the page to not function properly. Seems to me that
    stripping the HTML would be better, if you're not displaying the whole
    string.

    --
    Tony Garcia
    Web Right! Development
    Riverside, CA
    www.WebRightDevelopment.com


    Tony Guest

  6. #6

    Default Re: Cutting strings short with php

    Tony wrote: 
    >>
    >>How will that help him, since I guess he needs the HTML tags?!
    >>[/ref]
    >
    >
    > I could see that possibly causing some problems, since you would have
    > opening tags but no closing tags. Depending on the tags, of course:
    >
    > <pre>This is a string of more than 100 characters. This is a string of more
    > than 100 characters. This is a string of more than 100 characters. This is a
    > string of more than 100 characters. This is a string of more than 100
    > characters. This is a string of more than 100 characters.</pre>
    >
    > could cause the rest of the page to not function properly. Seems to me that
    > stripping the HTML would be better, if you're not displaying the whole
    > string.
    >[/ref]
    Thats true, but if you split the string like I proposed you could simply add closing tags
    to opened tags after cutting the string.

    Regards
    Stefan
    Stefan Guest

Similar Threads

  1. Extracting strings delimited by other strings
    By Scott Bass in forum PERL Modules
    Replies: 2
    Last Post: May 7th, 02:26 AM
  2. Replies: 8
    Last Post: September 3rd, 02:48 PM
  3. Replacing strings with strings?
    By Dave in forum Mac Programming
    Replies: 3
    Last Post: February 24th, 05:22 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139