Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 17th, 2003, 01:51 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old July 17th, 2003, 02:03 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
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/
Reply With Quote
  #3 (permalink)  
Old August 14th, 2003, 02:54 AM
Authorized User
 
Join Date: Aug 2003
Location: , , .
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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing the xml which is having vikkiefd XML 14 July 28th, 2008 06:21 AM
Error in php xml parsing... caught PHP How-To 0 April 12th, 2007 04:54 AM
Parsing mwviola98 SQL Language 4 July 21st, 2005 11: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 12:09 PM



All times are GMT -4. The time now is 10:49 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.