Avatar of Bob-Villa
Bob-Villa

asked on 

PERL Force String Wrapping

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?
Scripting LanguagesPerl

Avatar of undefined
Last Comment
ozo
Avatar of midrisi
midrisi

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;
Avatar of Bob-Villa
Bob-Villa

ASKER

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.
Avatar of nedfine
nedfine
Flag of India image

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";
Avatar of nedfine
nedfine
Flag of India image

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";
ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of FishMonger
FishMonger
Flag of United States of America image

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
Avatar of ozo
ozo
Flag of United States of America image

How about adding <wbr /> after $x non spaces followed by comma?
s#(\S{$x},)(?!\s)#$1<wbr />#g
Avatar of Bob-Villa
Bob-Villa

ASKER

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)

SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Scripting Languages
Scripting Languages

A scripting language is a programming language that supports scripts, programs written for a special run-time environment that automate the execution of tasks that could alternatively be executed one-by-one by a human operator. Scripting languages are often interpreted (rather than compiled). Primitives are usually the elementary tasks or API calls, and the language allows them to be combined into more complex programs. Environments that can be automated through scripting include software applications, web pages within a web browser, the shells of operating systems (OS), embedded systems, as well as numerous games. A scripting language can be viewed as a domain-specific language for a particular environment; in the case of scripting an application, this is also known as an extension language.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo