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
"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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<?php
function getTagText($textString, $tagName)
{
$tag_begin = $tagName;
$tag_end = (3 === func_num_args()) ? func_get_arg(2) : $tagName;
preg_match("/<{$tag_begin} [^>]*?>(.* ?)<\/?{$ta g_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"));
?>
function getTagText($textString, $tagName)
{
$tag_begin = $tagName;
$tag_end = (3 === func_num_args()) ? func_get_arg(2) : $tagName;
preg_match("/<{$tag_begin}
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,
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I totally agree with Roonaan. In this case strpos is better...
ASKER
well thanks guys. your input is of much help!
ASKER