Solved

# Substr

Posted on 1998-09-05
Medium Priority
291 Views
I have some srings like
<br><a href="test.htm"><b>Some Text</b></a>
and
<br><a href="testgetmore.htm"><b>More Text</b></a>

i want to get the substring after <b> and before </b> but the position of "<b>" is on a defrent position in the string
how do i get the position of the <b> and how do u use substr?

Thanks

0
Question by:delpro
• 2
• 2

LVL 85

Expert Comment

ID: 1204700
(\$substring) = \$string =~ m|<b>(.*?)</b>|;

#to get the position of the <b>
\$position = index \$string,'<b>';
#to use substr
\$substring = substr \$string,\$position+3;
#or
\$substring = substr \$string,\$position+3,(index \$string,'</b>')-\$position-3;
0

LVL 28

Accepted Solution

sybe earned 0 total points
ID: 1204701
sub ExtractBetween
{
local(\$string, \$tag1, \$tag2) = @_;
\$ind1 = index(\$string, \$tag1) + length(\$tag1);
\$ind2 = index(\$string, \$tag2);
\$string = substr(\$string,\$ind1,\$ind2-\$ind1);
return \$string;
}

\$yourstring = '<br><a href="test.htm"><b>Some Text</b></a>';
\$extractedstring = ExtractBetween(\$yourstring,'<b>','</b>');

0

LVL 85

Expert Comment

ID: 1204702
#If you're going to use it in a sub, then perhaps
\$ind2 = index(\$string, \$tag2);
#would be be safer as
\$ind2 = index(\$string, \$tag2, \$ind1);
#so that
ExtractBetween('</b><br><a href="test.htm"><b>Some Text</b>',<b>','<');
#still works

#although for literal tags,
(\$extractedstring) = \$yourstring =~ m|<b>(.*?)</b>|;
#still seems easier
0

LVL 28

Expert Comment

ID: 1204703
I wrote my answer as ozo posted his comment, so i did not see his first comment.
Anyway, I agree with ozo that his answer is easier.

ozo, you're a real perl-magician !

0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.