| | |||||
| ||||||||
| CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) | ||||||||||||
| Senior Member Join Date: Aug 2005 Location: Uk, South Yorkshire
Posts: 1,228
![]() ![]() ![]() ![]() ![]() ![]() ![]() | [PHP] Trim a string by length Hi, i wrote this today so though i'd share it. Trim a string to a certain length This function lets you trim a string to a specified length, either by character length, or by keeping whole words intact. Example We will be trimming this sentence...
PHP Code: str_trim( string, method, length, separator)
Default Method = WORDS, Length = 25, Separator = ... as default You only need to specify the string. PHP Code: UPDATED: It now adds the spaces into the character count, plus cleanup.
Last edited by Hitch; 10-25-2011 at 02:52 AM.
| ||||||||||||
| | |
| | #3 (permalink) |
| NamePros Regular Join Date: Mar 2007 Location: Constanta, Romania
Posts: 502
![]() ![]() ![]() ![]() | Or you can simply use: Code: $description = "Some example here"; echo '.substr($description, 0, 6).'; ????: NamePros.com http://www.namepros.com/showthread.php?t=712577 Code: Some e
__________________ Social networking aggregator |
| | |
| | #5 (permalink) |
| Member Join Date: Mar 2007
Posts: 1,358
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Nice sample. Thumbs up for posting. Respect. Personally i would use: strpos & strrev. something like: $MyStr="Hello there how are you" to cut to 14 1. $MyStr = substr($MyStr,1,14) // "Hello there ho" 2. $MyStr=strrev($MyStr) 3. $SpacePos=strpos($MyStr,' ') 3,5. $MyStr=strrev($MyStr) 4. $MyStr=(substr($MyStr,1,(strlen($MyStr)-$SpacePos))) (untested) But just my 2 cents ;-) |
| | |
| | #6 (permalink) |
| Tech Support Join Date: Mar 2005
Posts: 4,944
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | Nice function Adrian, made some changes ![]() PHP Code: |
| | |
| | #10 (permalink) |
| NamePros Regular Join Date: Jun 2011 Location: Moscow
Posts: 622
![]() ![]() ![]() ![]() ![]() ![]() ![]() | your algorithm is very ineffective - imagine you've got file with xx millions words and very long $length=100000, beginning from the start, cycling thru words/exploding large array is slow and resource demanding. much faster and clean way of doing the same thing would be imho: PHP Code: *sorry the thread is somewhat old - i didnt notice that anyway...
Last edited by 4pm; 02-19-2012 at 12:00 PM.
|
| | |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PHP Fuction - Parse a String + Extras | MJ | Web Development Wanted | 2 | 11-17-2008 09:56 AM |
| SEO and URL 2 (domain name part of search string?) | interestedenough | Search Engines | 2 | 04-02-2008 09:40 AM |
| encrypt and decrypt query string | sales@vannova.com | Programming | 5 | 06-06-2007 11:45 PM |
| Alternating string splits | Outer | Programming | 1 | 08-11-2005 03:05 AM |
| What length is your domain? | jaikini | Domain Name Discussion | 16 | 07-18-2004 07:32 AM |