Get text between elements with regex

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

LVL 2
xybxAsked:
Who is Participating?
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.

KennyTMCommented:
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
KennyTMCommented:
In fact it'd be better to use

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

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

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
tolgaongCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

ixtiCommented:
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
star_trekCommented:
use
 preg_match("|<text>(.*)</text>|Uis",$input,$matches);

print_r($matches);
0
BogoJokerCommented:
To make that not greedy add the ? after the .*:
 preg_match("|<text>(.*?)</text>|Uis",$input,$matches);
0
KennyTMCommented:
Re BogoJoker:
The /U flag marks ungreediness already. The extra ? makes the search greedy again (double negative = positve).
0
BogoJokerCommented:
Oh, didn't know that, hehe thanks =)
0
xybxAuthor Commented:
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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.