Solved

PERL Force String Wrapping

Posted on 2007-03-23
9
910 Views
Last Modified: 2013-12-25
I am having a problem with a string. I have a field that I let users type in free text. Some users don't use spaces.. i.e. sep everything with commas instead of spaces. When they do this and I display the content is messes up my page format. Is there a way to force a space or <br> tag every X characters using regex?
0
Comment
Question by:Bob-Villa
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:midrisi
Comment Utility
This may resolve your problem.I am replacing all commas with one single space.

#!/usr/local/bin/perl

$text="bangalore,new york,london                                     cunnighma,dehli";
$text=~ s/,/ /g;

print $text;
0
 
LVL 1

Author Comment

by:Bob-Villa
Comment Utility
I don't mind the commas and I don't want to replace every comma. adding a space after every consecutive length of $x characters would be the best solution for me.
0
 
LVL 8

Expert Comment

by:nedfine
Comment Utility
insert space  after 5 characters
$x=5;  #change this to any value
@a = split //;
for($i=0;$i<$#a;$i++)
{
push($a[$i],@b);
$j = $i % $x;
if($j == 0)
{
push(" ",@b);
}
}
print "\n @b";
0
 
LVL 8

Expert Comment

by:nedfine
Comment Utility
sorry thers a mistake....

$x=5;  #change this to any value
@a = split //;
for($i=0;$i<$#a;$i++)
{
push(@b,$a[$i]);
$j = $i % $x;
if($j == 0)
{
push(@b," ");
}
}
print "\n @b";
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Accepted Solution

by:
FishMonger earned 125 total points
Comment Utility
Adding a space after every consecutive length of $x characters would pretty much guarantee that it would add a space where it's not supposed to, such as in the middle of a word.

How about adjusting midrisi's suggestion and add a space after every comma?

$text=~ s/,/, /g;
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
If you really want to add the space at every x length, then a simple regex would suffice.

$text="bangalore,new york,london,cunnighma,dehli";

$x = 5;
$text =~ s/\G(.{$x})/$1 /g;
print $text;

----

outputs:
banga lore, new y ork,l ondon ,cunn ighma ,dehl i
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
How about adding <wbr /> after $x non spaces followed by comma?
s#(\S{$x},)(?!\s)#$1<wbr />#g
0
 
LVL 1

Author Comment

by:Bob-Villa
Comment Utility
FishMonger's  $text=~ s/,/, /g; is great (can't believe I didn't think of that) but the only problem is with numbers (i.e. 3,000) I will almost never have part of the string have a 2 numbers sep by a comma unless it is a number greater than 999. I know this is sloppy but it will solve 99% of my issues.

sample string:
THISIS3,000,abcdef

THISIS3,000, abcdef (OK - this would be perfect)
THISIS3, 000, abcdef (NOT OK)

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 125 total points
Comment Utility
$text=~ s/,(?!\d\d\d)/, /g;
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This tutorial will discuss the log-in process using WhizBase. In this article I assume you already know HTML. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you might look at some of my other articles abo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now