Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Get text between elements with regex

Posted on 2006-04-18
9
Medium Priority
?
417 Views
Last Modified: 2008-01-09
OK, this ought to be a piece of cake, but for whatever reason, it's become a big deal. I am simply trying to return text between two elements.

For example, suppose I have a form, and it contains

<text>
whatever I want here

maybe there are line breaks
</text>

Now, using PHP, I ought to be able to grab what is in the <text> tags and do other stuff with it.

I've tried madness like, preg_match_all("/(<text>blah<\/text>)/",$form_input,$matches) and then looping through it, to no avail. Note that it won't even match on '<t'. I'm [obviously?] new to PHP, and I have read the docs for this, but regardless of my variations, nothing has worked.

thanks for the help

0
Comment
Question by:xybx
9 Comments
 
LVL 8

Expert Comment

by:KennyTM
ID: 16484684
Hi.

Try

$form_input = '3453535<text>2342345345423</text>asdfafsf<text>2342cvxcv423</text>sfsdfsdf';
 preg_match_all("|<text>(.*)</text>|U",$form_input,$matches);
print_r ($matches); // should display the text between the <text>'s.
0
 
LVL 8

Accepted Solution

by:
KennyTM earned 375 total points
ID: 16484689
In fact it'd be better to use

 preg_match_all("|<text>(.*)</text>|Ui",$form_input,$matches);

because the <text> maybe in "UPPERCASE".
0
 
LVL 9

Assisted Solution

by:tolgaong
tolgaong earned 375 total points
ID: 16484868
preg_match_all('/<text>(?P<textvalue>[\\n\\r\\s\\w\\d\\W]*)<\/text>/i', $subject, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
      echo $result[0][$i];
}
0
Technology Partners: 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!

 
LVL 6

Expert Comment

by:ixti
ID: 16485570
Very closer to what you want was discussed here: http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21817029.html

If I clearly understood, then you want to parse something posted through your form. Then the posted data will be in variable, for example:
<?php
$_POST['message'] = "<text>
whatever I want here

maybe there are line breaks
</text>";
?>

So you can try to use Roonaan's variant:
<?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 '';
}

var_dump(getTagText($_POST['message'], 'text'));
?>

Or if, you want to extract text from all tags, for example some one posted data with more tan one tag-sets:
<?php
$_POST['message'] = "<text>
whatever I want here

maybe there are line breaks
</text><text> another text
goes here</text>";
?>

You can modiify my variant so it would be like this:
<?php
function getTagText($textString, $tagName)
{
    $tag_begin = $tagName;
    $tag_end = (3 === func_num_args()) ? func_get_arg(2) : $tagName;
    preg_match_all("/<{$tag_begin}[^>]*>(?P<text>.*)<\/{$tag_end}>/Usi", $textString, $matches, PREG_PATTERN_ORDER);
    return $matches["text"];
}

var_dump(getTagText($_POST['message'], 'text'));
?>


PS The only one thing you got to fix or notice - it do not understand something like "<text> some text <text>cascade goes...</text> here</text>" So you got to note this, or simply modify it so it could understand this. It's not hard to do.
0
 
LVL 11

Expert Comment

by:star_trek
ID: 16487567
use
 preg_match("|<text>(.*)</text>|Uis",$input,$matches);

print_r($matches);
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16489645
To make that not greedy add the ? after the .*:
 preg_match("|<text>(.*?)</text>|Uis",$input,$matches);
0
 
LVL 8

Expert Comment

by:KennyTM
ID: 16489670
Re BogoJoker:
The /U flag marks ungreediness already. The extra ? makes the search greedy again (double negative = positve).
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16489680
Oh, didn't know that, hehe thanks =)
0
 
LVL 2

Author Comment

by:xybx
ID: 16492499
Thanks everyone for the help. I ended up solving this one like I needed..

Here's what I ended up with after too much work.

function ReadBetweenTags($tag,$strInput) {
      global $orig;
      $test = $orig;
      $pattern = "|^([\w\W\r\n]*?)<".$tag.">([\w\W\r\n]*?)</".$tag.">([\w\W\r\n]*)$|Ui";
      
      while(preg_match($pattern, $test, $result)) {
            $before = $result[1];
            $match = $result[2];
            $after = $result[3];
            $test="$before<pre class=\"text\">".HTMLOutput($match)."</pre>$after";
      }
      return $test;
}

I'll just divy up the points for those whom I felt helped the most.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

578 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