?
Solved

Extracting the text between two given tags or patterns.

Posted on 2006-04-17
7
Medium Priority
?
481 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:markdoc
7 Comments
 
LVL 1

Assisted Solution

by:uworks
uworks earned 600 total points
ID: 16474510
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
 
LVL 4

Author Comment

by:markdoc
ID: 16474531
thanks ill try it now...
0
 
LVL 6

Expert Comment

by:ixti
ID: 16475080
<?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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Assisted Solution

by:ixti
ixti earned 600 total points
ID: 16475091
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
 
LVL 49

Accepted Solution

by:
Roonaan earned 800 total points
ID: 16475404
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
 
LVL 6

Expert Comment

by:ixti
ID: 16475445
I totally agree with Roonaan. In this case strpos is better...
0
 
LVL 4

Author Comment

by:markdoc
ID: 16476040
well thanks guys. your input is of much help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question