Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2003, 12:51 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parsing in PHP

Hello I am trying to extract some data from a string using PHP. I am used to doing this in Perl.

This is a sample of the data format.
^UU1877778^Uk665544^UC^PC2^UfYTR^YUY^^Z

The data represents user data and is variable length from record to record.

I would like some code that will extract the data between delimiters. For example, between ^Uk and ^UC the data returned would be 665544.

In perl you would have a pattern match something like.
$data ~= /*.\^Uk(.*?)\^UC.*/;
$extract = $1;
$extract would equal 665544.

The data in brackets will get assigned to $1.

Is there a way of doing this in PHP? I have done something close using $num = preg_match_all('/\^UU.+\^Uk/', $searchdata, $match, PREG_PATTERN_ORDER);

This kind of works but it includes the ^UU and ^Uk as part of the match which I then have to strip off later. Sometimes my delimiter maybe ^^Z. I have read two PHP books and so far have not found a Perl like or other easy way of doing this.


Walter G.
 
Old July 17th, 2003, 01:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The only thing you need to modify in your preg_match pattern is to add a parenthesized subexpression. preg_match() will return the entire string if the expression matches; essentially providing a non-false value signifying a successful match. Each parenthesized subexpression, if any, that matches is also returned.

Play with print_r() and/or var_dump() when dealing with the return values of preg_match functions to get a feel for the structure of the return value as it relates to a parenthesized pattern.

$pattern = '/^UU(.*)^Uk/';
$num = preg_match_all($pattern, $searchdata, $matches);

echo "<pre>Matches are:";
print_r($matches);
echo "</pre>\n";


Take care,

Nik
http://www.bigaction.org/
 
Old August 14th, 2003, 01:54 AM
Authorized User
 
Join Date: Aug 2003
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if i understand your problem correctly, you want to go from :
^a^b^c^d^
to
array ( 'a','b','c','d' )

if so, you can user the explode function using '^' as a separator :

$myData = "^a^b^c^d^";
$myNewData = explode( "^", $myData );

//to display
echo "<pre>";
print_r( array_values($myNewData) );
echo "</pre>";

php/java developer
NTIC engineer





Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing the xml which is having vikkiefd XML 14 July 28th, 2008 05:21 AM
Error in php xml parsing... caught PHP How-To 0 April 12th, 2007 03:54 AM
Parsing mwviola98 SQL Language 4 July 21st, 2005 10:35 AM
Parsing URL with PHP turklet BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 November 27th, 2004 09:24 PM
Hidden redirect but no php parsing klokkie Beginning PHP 2 August 5th, 2004 11:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.