Click to See Complete Forum and Search --> : using the substitution regex


chuckdawit
06-28-2007, 05:40 PM
I have a string containing two url's separated by a <br/>. I would like to search through the string and replace/substitute the name in $sr with the same name in $string only in the color red and in bold. Here is what I'm trying:

my $sr = "2htec";
my $string = 'http://www.2htec.com <br/> http://www.2htec.com.cn';

while ($string =~ =~ m/$sr/gio) {
$string =~ s/$sr/"<font size='2' color='red'><b>" . $sr . "</font>"/gio;
}

print $string;


I can replace the string "$sr" with any string like cat, but I can't include the html. Any help with this would be appreciated. Thanks-Chuck

CyCo
06-28-2007, 06:53 PM
Lose the while loop and use this:$string =~ s/$sr/<font size=\"2\" color=\"red\"><b>$sr<\/b><\/font>/gio;But, rather than using deprecated HTML tags, though, you might want to use inline style definitions like so:$string =~ s/$sr/<span style=\"color:#f00;font-weight:bold;font-size:smaller\">$sr<\/span>/gio;Or even something like this, for brevity:$string =~ s/$sr/<span class=\"wow\">$sr<\/span>/gio;And use:span.wow {
color: #f00;
font-weight: bold;
font-size: smaller;
}in your internal or external style sheet.

chuckdawit
06-28-2007, 08:03 PM
ok thanks. I got it. My subroutine passes two strings and replace the string found with bold text and red color and returns the string with red and bold.
Here is what I did:
sub highlight
{
my $word = shift; #word from db
my $sr = shift; #search string user enters

if ($word =~ s/$sr/<font size=\"2\" color=\"red\"><b>$sr<\/b><\/font>/gio) {}
else {
return $word;
}
return $word;
}