I want to turn this string (entered in a form textArea):
`bhey` man `bwhassup`
into this:
<b>hey</b> man <b>whassup</b>
Here is my attempt in Perl, which isn't working right:
# TESTING EZ FORMATTING
$boldBegin = "`b";
$ezEnd = "`";# $CONTENT IS FROM THE FORM TEXTAREA
while($theContent =~ /($boldBegin.+$ezEnd)/){
$found = $1;
$myFound = $found;
$theReplacement = $myFound;$theReplacement =~ s/\`b//g;
$theReplacement =~ s/\`//g;
$theReplacement = "<b>" . $theReplacement . "</b>";$theContent =~ s/$myFound/$theReplacement/im;
}
The problem is that $theFound contains the whole thing:
`bhey` man `bwhassup`
instead of just the first instance of $boldBegin.+$ezEnd, which would be
`bhey`
and the whole thing keeps looping.
How do I say "find these occurrences, however many there are, ONE AT A TIME and change them"?
Thanks much!
Perry
# TESTING EZ FORMATTING
$boldBegin = "`b";
$ezEnd = "`";
$theContent= "`bhey` man `bwhassup`";
# $CONTENT IS FROM THE FORM TEXTAREA
while($theContent =~ /($boldBegin.+$ezEnd)/){
$found = $1;
print $found . "\n";
$myFound = $found;
$theReplacement = $myFound;
$theReplacement =~ s/\`b/<b>/g;
$theReplacement =~ s/\`/<\/b>/g;
$theContent =~ s/$myFound/$theReplacement/im;
print $theContent . "\n";
}
I becomes easy for you in the code if you can use say:
'b b' for start and end tags. It won't be perfect but it will solve the finding of end tags issue.
so your new code will look like this:
use strict;
my $boldBegin = "'b";
my $boldEnd = "b'";
$content =~ s¦$boldBegin¦<b>¦gi;
$content =~ s¦$boldEnd¦</b>¦gi;
#pipe symbol used so that there is no confusion with
#closing tags.
I may have oversimplified your issue. I am not sure if you are trying to do something bigger....
The reason the the .+ is grabbing the entire area is that the + quantifier is looking for as many of the given match as it can. You need to quantify with something more specific.
Match any but an end tag (in the context of your example)
use strict;my $content="'bhey' man 'bwhassup'";
my $boldBegin = "'b";
my $ezEnd = "'";
$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;
print $content;
Again, I have changed the separators so that the code just looks clearer and we don't have to escape anything.
`bhey` man `iwhassup`
would become
<b>hey</b> man <i>whassup</i>
Less typing, and no shift keys.
MarkAnthony,
Thanks much for the code.
my $content="'bhey' man 'bwhassup'";my $boldBegin = "'b";
my $ezEnd = "'";$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;
print $content;
It works when "my $content" = the literal string, but doesn't work when it = the form input (a textarea containing the same text as the literal).
my $content=$FORM{'content'};my $boldBegin = "'b";
my $ezEnd = "'";$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;
print $content; #PRINTS OUT `bhey` man `bwhassup`
Any thoughts on how I can correct this?
Thanks!
Perry
I have another question for you:
$content =~ s!$boldBegin([^$ezEnd]+)$ezEnd{1}!<b>$1</b>!gi;
This works when $ezEnd is a single character (which is what I originally asked for). How could I modify this to allow for an $ezEnd that has more than one character?
Thanks again!
Perry