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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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);
markdocAuthor Commented:
thanks ill try it now...
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"));
?>
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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"));
?>
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 '';
}
?>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ixtiCommented:
I totally agree with Roonaan. In this case strpos is better...
markdocAuthor Commented:
well thanks guys. your input is of much help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.