Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PREG_MATCH 1

Status
Not open for further replies.

FinnMan

Technical User
Feb 20, 2001
75
US
I have a variable $result (for sake of discussion) that contains a bunch of html. I'm trying to grep out a particular section to assign to a another variable. My HTML likes the following:

***************************
<br>TEXT: data<br><br></body>
***************************

I simply want to put 'data' into a variable. I've gotten to where I can grep for TEXT to check my result but I'm not sure how to parse out 'data'.

Thx in advance for your suggestions.

./FM
 
From el manual

strstr
(PHP 3, PHP 4 , PHP 5)

strstr -- Find first occurrence of a string
Description
string strstr ( string haystack, string needle)


Returns part of haystack string from the first occurrence of needle to the end of haystack.

If needle is not found, returns FALSE.

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

Note: This function is case-sensitive. For case-insensitive searches, use stristr().

Example 1. strstr() example

<?php
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?>


 
That will return ' TEXT: data ' when I only want to return 'data'. Is there a simple method to pull just data or should I use strstr and chop it?
 
Code:
$string = "<br>TEXT: data<br><br></body>";
$res = strip_tags($string); // Value should be TEXT: data
$res = preg_replace("/TEXT: /","",$res);
echo $res; // should echo data

Or

Code:
$string = "<br>TEXT: data<br><br></body>";
$len = str_len($string);
$res = "";
$found = false;
for ($i=0; $i<$len; $i++)
{
  if (!$found)
  {
    if (substr($string,$i-6,$i) == "TEXT: ")
    {
      $found = true;
    }
  }
  else
  {
    $char = substr($string,$i-1,$i)
    if ($char != "<");
    {
      $res .= $char;
    }
    else
    {
      break;
    }
  }
}

echo $res;

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top