Link to home
Start Free TrialLog in
Avatar of markdoc
markdoc

asked on

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:

https://www.experts-exchange.com/questions/21816007/Extracting-the-text-between-two-given-tags-or-patterns.html#16468198
SOLUTION
Avatar of uworks
uworks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of markdoc
markdoc

ASKER

thanks ill try it now...
<?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"));
?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I totally agree with Roonaan. In this case strpos is better...
Avatar of markdoc

ASKER

well thanks guys. your input is of much help!