Extracting the text between two given tags or patterns.

hi guys, i want to extract a string of arbitrary length between two given tags. for example, i have the string

"some string here <beginning_tag>the string i want to extract<end_tag>some more string here"

i want to extract the string between the beginning_tag and ending_tag. is there a way that i can do this with a minimal amount of code, say with regular expressions? any help/suggestions will be appreciated.

i asked something like this in the general programming section, but i want to do something like this particulary in php:

http://www.experts-exchange.com/Programming/Q_21816007.html#16468198
LVL 4
markdocAsked:
Who is Participating?
 
RoonaanCommented:
As an alternative to ixti's code, stripos might be faster performance-wise.

-r-

<?php
function getTagText($textString, $tagName)
{
    $tag_begin = $tagName;
    $tag_end = (3 === func_num_args()) ? func_get_arg(2) : $tagName;
    $start = stripos($textString, '<'.$tag_begin.'>')+strlen($tag_begin)+2;
    $end  = stripos($textString, '</'.$tag_end.'>', $start);
    if($start && $end) return substr($textString, $start, $end-$start);
    return '';
}
?>
0
 
uworksCommented:
Please try:

$texttosearch = "some string here <beginning_tag>the string i want to extract</end_tag>some more string here";

eregi("<beginning_tag[^>]*?>.*?</end_tag>", $texttosearch);
0
 
markdocAuthor Commented:
thanks ill try it now...
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
ixtiCommented:
<?php
function getTagText($textString, $tagName)
{
    $tag_begin = $tagName;
    $tag_end = (3 === func_num_args()) ? func_get_arg(2) : $tagName;
    preg_match("/<{$tag_begin}[^>]*?>(.*?)<\/?{$tag_end}>/", $textString, $matches);
    return $matches[1];
}

$text = "some string here <beginning_tag>the string i want to extract<end_tag>some more string here";
var_dump(getTagText($text, "beginning_tag", "end_tag"));
?>
0
 
ixtiCommented:
PS If third param not set then end_tag will be as beginnig_tag
PPS If you want that end tags was always closed then this function should be like this:
<?php
function getTagText($textString, $tagName)
{
    $tag_begin = $tagName;
    $tag_end = (3 === func_num_args()) ? func_get_arg(2) : $tagName;
    preg_match("/<{$tag_begin}[^>]*?>(.*?)<\/{$tag_end}>/", $textString, $matches);
    return $matches[1];
}

$text = "some string here <bold>the string i want to extract</bold>some more string here";
var_dump(getTagText($text, "bold"));
?>
0
 
ixtiCommented:
I totally agree with Roonaan. In this case strpos is better...
0
 
markdocAuthor Commented:
well thanks guys. your input is of much help!
0
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.

All Courses

From novice to tech pro — start learning today.