RE: [PHP] Text between two tags

2005-11-10 Thread mike bellerby

try explode using td align=right class=yfnc_tabledata1 then on array 
index[1] explode /td 
your answer will then be in index[0]

Hope this helps

Mike



 Message Received: Nov 10 2005, 03:01 PM
 From: Normmy2k [EMAIL PROTECTED]
 To: php-general@lists.php.net
 Cc: 
 Subject: [PHP] Text between two tags
 
 Hello
 I'm trying to extract some text between two tags:
 
 Example
 I have this string :
 $text='td/td
 td align=right class=yfnc_tabledata3/td
 td/td
 td align=right class=yfnc_tabledata2/td
 td/td
 td align=right class=yfnc_tabledata11234/td
 td/td td/td td/td td align=right class=yfnc_tabledata5/td 
';
 
 And I want to retrieve the number 1234 between td align=right
 class=yfnc_tabledata1 and /td
 Any idea??
 
 Normmy.
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Text between two tags

2005-11-10 Thread Leonard Burton
HI,

 Hello
 I'm trying to extract some text between two tags:

I am answering your question from the perspective that you are going
to have more than one column that you want the data from.

 I would use this pattern and this code to test the pattern.
?

$pattern=/(td align=\(.*)\ class=\(.*)\(.*)\/td)/;
$text='td/td
td align=right class=yfnc_tabledata3/td
td/td
td align=right class=yfnc_tabledata2/td
td/td
td align=right class=yfnc_tabledata11234/td
td/td td/td td/td td align=right class=yfnc_tabledata5/td ';


preg_match_all($pattern, $text, $matches);

print_r($matches);

?

This will give you:

[0] = Array
(
[0] = td align=right class=yfnc_tabledata3/td
[1] = td align=right class=yfnc_tabledata2/td
[2] = td align=right class=yfnc_tabledata11234/td
)

[1] = Array
(
[0] = td align=right class=yfnc_tabledata3/td
[1] = td align=right class=yfnc_tabledata2/td
[2] = td align=right class=yfnc_tabledata11234/td
)

[2] = Array
(
[0] = right
[1] = right
[2] = right
)

[3] = Array
(
[0] = yfnc_tabledata3
[1] = yfnc_tabledata2
[2] = yfnc_tabledata1
)

[4] = Array
(
[0] =
[1] =
[2] = 1234
)


THen you can loop through $matches[4] and you will get all of the
values in your row.  $mathces[3] will give you your class names and
$matches[2] will give you the alignment and $matches[1] will give you
the whole td blah blah blah /td string.

You could explode on tr and get each new row of your table in a new
element in an array and repeat the process for your entire table.

Do note that my regular expression will only return columns with a
class name and align attribute.

You can use $matches[0] elements if you are needing to replace what is
there with other information.

I hope this helps.  Please let me know if it does what you are asking
for as I am in need of good regular expression practice.

If you are needing to alter my regex some you can use
http://www.quanetic.com/regex.php to help.



--
Leonard Burton, N9URK
[EMAIL PROTECTED]


The prolonged evacuation would have dramatically affected the
survivability of the occupants.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Text between two tags

2005-11-10 Thread tg-php
Maybe just treat it like XML and use PHP's XML parsing routines.  Should work 
pretty good but maybe someone else has a better way of handling it. (code below)

-TG


// $string - contains your HTML string

$innerText = ;

$_XML_PARSER = xml_parser_create();
xml_set_element_handler($_XML_PARSER,'xml_open_element_function','xml_close_element_function');
xml_set_character_data_handler($_XML_PARSER,'xml_handle_character_data');
xml_parse($_XML_PARSER,$string,strlen($string));
xml_parser_free($_XML_PARSER);

echo $innerText;

function xml_response_open_element_function($p, $element, $attributes){
  global $grabInnerText;

  if (strtolower($element) == td AND $attributes['class'] == 
yfnc_tabledata1) {
$grabInnerText = true;
  } else {
$grabInnerText = false;
  }
}

function xml_response_close_element_function($p, $element){
  global $grabInnerText;

  // Probably unnecessary?
  $grabInnerText = false;
}

function xml_response_handle_character_data_pdf($p, $cdata){
  global $grabInnerText;
  global $innerText;

  if ($grabInnerText) {
$innerText .= $cdata;
  }
}


= = = Original message = = =

Hello
I'm trying to extract some text between two tags:

Example
I have this string :
$text='td/td
td align=right class=yfnc_tabledata3/td
td/td
td align=right class=yfnc_tabledata2/td
td/td
td align=right class=yfnc_tabledata11234/td
td/td td/td td/td td align=right class=yfnc_tabledata5/td ';

And I want to retrieve the number 1234 between td align=right
class=yfnc_tabledata1 and /td
Any idea??

Normmy.


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Text between two tags

2005-11-10 Thread tg-php
Great example.. haha.. I knew someone would pull some nice regex out for this 
one.. I suck at regex. hah

I still like my XML parsing fix, but Leonard's regex is probably better. hah

-TG

= = = Original message = = =

HI,

 Hello
 I'm trying to extract some text between two tags:

I am answering your question from the perspective that you are going
to have more than one column that you want the data from.

 I would use this pattern and this code to test the pattern.
?

$pattern=/(td align=\(.*)\ class=\(.*)\(.*)\/td)/;
$text='td/td
td align=right class=yfnc_tabledata3/td
td/td
td align=right class=yfnc_tabledata2/td
td/td
td align=right class=yfnc_tabledata11234/td
td/td td/td td/td td align=right class=yfnc_tabledata5/td ';


preg_match_all($pattern, $text, $matches);

print_r($matches);

?

This will give you:

[0] = Array
(
[0] = td align=right class=yfnc_tabledata3/td
[1] = td align=right class=yfnc_tabledata2/td
[2] = td align=right class=yfnc_tabledata11234/td
)

[1] = Array
(
[0] = td align=right class=yfnc_tabledata3/td
[1] = td align=right class=yfnc_tabledata2/td
[2] = td align=right class=yfnc_tabledata11234/td
)

[2] = Array
(
[0] = right
[1] = right
[2] = right
)

[3] = Array
(
[0] = yfnc_tabledata3
[1] = yfnc_tabledata2
[2] = yfnc_tabledata1
)

[4] = Array
(
[0] =
[1] =
[2] = 1234
)


THen you can loop through $matches[4] and you will get all of the
values in your row.  $mathces[3] will give you your class names and
$matches[2] will give you the alignment and $matches[1] will give you
the whole td blah blah blah /td string.

You could explode on tr and get each new row of your table in a new
element in an array and repeat the process for your entire table.

Do note that my regular expression will only return columns with a
class name and align attribute.

You can use $matches[0] elements if you are needing to replace what is
there with other information.

I hope this helps.  Please let me know if it does what you are asking
for as I am in need of good regular expression practice.

If you are needing to alter my regex some you can use
http://www.quanetic.com/regex.php to help.



--
Leonard Burton, N9URK
[EMAIL PROTECTED]


The prolonged evacuation would have dramatically affected the
survivability of the occupants.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Text between two tags

2005-11-10 Thread Richard Lynch
On Thu, November 10, 2005 8:56 am, Normmy2k wrote:
 Example
 I have this string :
 $text='td/td
 td align=right class=yfnc_tabledata3/td
 td/td
 td align=right class=yfnc_tabledata2/td
 td/td
 td align=right class=yfnc_tabledata11234/td
 td/td td/td td/td td align=right
 class=yfnc_tabledata5/td ';

 And I want to retrieve the number 1234 between td align=right
 class=yfnc_tabledata1 and /td

If that string is consistently what you have, then this would also work:

$number = (int) trim(strip_tags($text));

This may prove more resilent to HTML changes from the source that is
generating all those table tags...

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php