RE: [PHP] Need help with RegEx
| From: | Brad Fuller | Date: | Mon, 11 Dec 2006 16:55:00 +0000 |
| Subject: | RE: [PHP] Need help with RegEx | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-245711@lists.php.net to get a copy of this message | ||
I got it.
<?php
$input = "<xmlJunk><status>Hello, World!</status></xmlJunk>";
preg_match("#<status>(.*?)</status>#s", $input, $matches);
echo $matches[1];
?>
> -----Original Message-----
> From: tg-php@gryffyndevelopment.com [mailto:tg-php@gryffyndevelopment.com]
> Sent: Monday, December 11, 2006 10:59 AM
> To: php-general@lists.php.net
> Cc: bfuller@cpacampaigns.com
> Subject: RE: [PHP] Need help with RegEx
>
> If you didn't say "using regex" this is how I'd do it (untested, forgive
> typos and such..ripped from some code I actively use and stripped down):
>
> <?PHP
>
> $_XML_RESPONSE_PARSER = xml_parser_create();
> xml_set_element_handler($_XML_RESPONSE_PARSER,
> 'xml_response_open_element_function',
> 'xml_response_close_element_function');
> xml_set_character_data_handler($_XML_RESPONSE_PARSER,
> 'xml_response_handle_character_data');
> xml_parse($_XML_RESPONSE_PARSER, $_XML_RESPONSE,
> strlen($_XML_RESPONSE));
> xml_parser_free($_XML_RESPONSE_PARSER);
> ~
> $FoundStatusTag = false;
> ~
> function xml_response_open_element_function($p, $element, $attributes) {
> global $FoundStatusTag;
> ~~
> if (strtoupper($element) == "STATUS") $FoundStatusTag = true;
> }
> ~
> function xml_response_close_element_function($p, $element){
> global $FoundStatusTag;
> ~
> // do nothing special for now
> }
> ~
> function xml_response_handle_character_data($p, $cdata){
> global $FoundStatusTag;
> ~
> if ($FoundStatusTag) {
> echo $cdata;
> $FoundStatusTag = false;
> }
> }
>
> ?>
>
> = = = Original message = = =
>
> The example provided didn't work for me. It gave me the same string
> without
> anything modified.
>
> I am also looking for this solution to strip out text from some XML
> response
> I get from posting data to a remote server. I can do it using substring
> functions but I'd like something more compact and portable. (A one-liner
> that I could modify for other uses as well)
>
> Example 1:
> <someXMLtags>
> ~<status>16664 Rejected: Invalid LTV</status>
> </someXMLtags>
>
> Example 2:
> <someXMLtags>
> ~<status>Unable to Post, Invalid Information</status>
> </someXMLtags>
>
> I want what is inside the <status> tags.
>
> Does anyone have a working solution how we can get the text from inside
> these tags using regex?
>
> Much appreciated,
>
> B
>
>
> ___________________________________________________________
> 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
>