[San-Diego-pm] Text substitution
Joel Fentin
joel at fentin.com
Mon May 1 14:31:58 PDT 2006
The following code adds bold tags to the desired text. But it also
changes the case.
$Text = 'Aaa bbb AAA bbb aaa'; #original text
$SearchFor = 'aAa'; #what we're looking for
$Text =~ s/$SearchFor/<b>$SearchFor<\/b>/gi; #disp search text bold
There might be a nifty regular expression that adds the tags but
inflicts no change of case. I haven't figured it out.
=================================
I solved the problem with this unhandy code:
$Where = length($Text); #start search at end of string
$CopyOfText = lc($Text); #make a low case copy
$CopyOfSearchFor = lc($SearchFor); #make a low case copy
$SearchForLength = length($SearchFor);
while($Where > -1) #loop while desired text is found
{
$Where = rindex($CopyOfText,$CopyOfSearchFor,$Where); #locate desired
text in string
if($Where > -1) #if desired text is in string...
{
$ExistingText = substr($Text, $Where, $SearchForLength); #get a
copy of it
substr($Text, $Where, $SearchForLength) = "<b>$ExistingText</b>";
#add bold tags to it
$Where--; #next search starts 1 chr to left
}
}
--
Joel Fentin tel: 760-749-8863 FAX: 760-749-8864
Email me: http://fentin.com/me/ContactMe.html
Biz Website: http://fentin.com
Personal Website: http://fentin.com/me
More information about the San-Diego-pm
mailing list