473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Remove x characters from the middle of a string.

Markus
6,050 Expert 4TB
I'm stuck on a little bit of logic.

Imagine, if a string's length, l, exceeds some specified maximum length, m. How could you then remove the correct amount of characters from the center of the string (replace with ellipsis: ...) so that it is less than or equal to the maximum length.

m = 20
string = averylongstringover20chars

would become: averylongs...ver20chars

I'm befuddled on this one.
Apr 24 '09 #1
1 4778
code green
1,726 Expert 1GB
I wrote this 2-3 years ago that did similar to what you are asking (I think)
Not making any great claims on how it works but maybe will help you.
I used it as part of a search engine.
I think BLOCK is a constant int.
highlight() is another function that colours matching terms red
Expand|Select|Wrap|Line Numbers
  1. function parseText($text,$findme)
  2. {
  3.     $textArray = preg_split ('/ /', $text );
  4.     $words = sizeof($textArray);
  5.  
  6.     $side = (int)(BLOCK/2);
  7.     $line = '';
  8.  
  9.     for($key=0;$key<$words;$key++)
  10.     {
  11.         if($key<$side)
  12.             $start = 0;
  13.         else
  14.             $start = $key-$side;
  15.         if(($start+BLOCK)>$words)
  16.             $start = $words-BLOCK;
  17.  
  18.         $pos = stripos($textArray[$key], $findme);
  19.         if ($pos !== false)
  20.         {
  21.             $chunk =  array_slice ($textArray,$start,BLOCK);
  22.             $chunk = implode($chunk,' ');
  23.  
  24.             //Add Ellipses
  25.             if($start>0)
  26.                 $line .= '.....';
  27.            if($words>($start+BLOCK))
  28.                 $chunk .= '.....';
  29.             $line .= $chunk;
  30.             $line .= '<br>';
  31.         }
  32.     }//for
  33.     if($line!='')
  34.         $display = highlight($line,$findme);
  35.     //No matches found so just truncate text
  36.     else
  37.     {
  38.         $chunk =  array_slice ($textArray,0,BLOCK);
  39.         $display = implode($chunk,' ');
  40.         $display .= '.....';
  41.     }
  42.     return $display;
  43. }
Apr 24 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
11
by: deko | last post by:
I need to loop through a string and remove all characters except numbers or letters. I am getting an ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
9
by: Smiley | last post by:
Hi, Can someone tell me how to remove aspostophe (') from user input. I don't want to give any error message. Just want to remove or change it to "" or null, such input when it appear anywhere...
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
4
by: mdthom08 | last post by:
I need to return the middle 2 characters of a word, favoring the right. so for example: SevenMethods myFuns = new SevenMethods(); assertEquals("34", myFuns.middleTwo("12345")); so far i...
1
by: Brock | last post by:
Thanks in advance... I have a string that I'm building that needs to write to a file the last name, first name and middle initial in this format with a maximum of 20 characters. Two examples: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.