HELP! Problem with string parsing!?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

HELP! Problem with string parsing!?

Post by visionmaster »

Hello,

I have a big problem with Strings, have tried to find the error for hours, without any result!


I. Script that works fine:
----------------------------


PHP-Code:

Code: Select all

<?php
    $array[3] = '76534 Baden-Baden';

    print gettype($array[3]).'<br>';
    $strPlz = substr($array[3],0,4);
    $strOrt = strstr($array[3],' ');
    
    print "PLZ=".$strPlz."<br>";
    print "Ort=".$strOrt;
?>

Remarks: PLZ is the German ZIP-Code for a city. Ort ist the city name.
=> Output:
string
PLZ=7653
Ort= Baden-Baden
=>Great! PLZ and Ort have sucessfully been parsed out of the string.

II. Script which doesn't work:
----------------------------------


PHP-Code:

Code: Select all

<?php
...
writeDataIntoDatabase($output_array_unique);
...
function writeDataIntoDatabase($array)
{

    global $idLink;
     
    print gettype($array[3]).'<br>';
    $strPlz = substr($array[3],0,4);
    $strOrt = strstr($array[3],' ');
    
    print "PLZ=".$strPlz."<br>";
    print "Ort=".$strOrt;
    
}
?>

=> Output:
string
PLZ=
Ort= 76534 Baden-Baden
==> HELP! Why doesn't the same thing happen like in script I? I don't understand this at all!!!
==> Why is $strPlz empty and $strOrt not?

Thanks for your help,
visionmaster
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Looks like $array[3] has spaces in it before the text.
Try:

Code: Select all

function writeDataIntoDatabase($array)
{

    global $idLink;
    $array[3] = trim($array[3]);
    //...rest of your code here...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

may want to look into regular expressions to do this:

Code: Select all

function writeDataIntoDatabase($array)
{
  global $idLink;
  preg_match('#^\s*([^\s]+?)\s+(.*?)\s*$#s', $array[3], $matches);
  $strPlz = $matches[1];
  $strOrt = $matches[2];

  // ...............
}
[edit: oops.. forgot to close the php tag :D]
Last edited by feyd on Wed Jul 21, 2004 2:16 pm, edited 1 time in total.
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

feyd wrote:may want to look into regular expressions to do this:

Code: Select all

function writeDataIntoDatabase($array)
{
  global $idLink;
  preg_match('#^\s*([^\s]+?)\s+(.*?)\s*$#s', $array[3], $matches);
  $strPlz = $matches[1];
  $strOrt = $matches[2];

  // ...............
}
Thanks for both of your quick responses! I'll try out the trim() solution tommorrow. I guess it will solve my problem...

Could you please give a short explanation to the above expression? I've done some things with RegExps, but above expression looks like Chinese to me... ;-)

Another example:
DE-72762 Baden-Baden
What regular expression can split it into
72762
and
Baden-Baden
(DE- can be discarded)

Regards from Germany
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

strips the surround spaces, stores the first word (from front of line to first space), then jumps to the next word and stores the rest of the string..
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

feyd wrote:strips the surround spaces, stores the first word (from front of line to first space), then jumps to the next word and stores the rest of the string..
Hi, thanks for the explanation. Understood some parts, but unfortunately not all of the expression parts. Could you please get into the details?

Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Okay I'll walk through the exacts of it..

Code: Select all

#^\s*(&#1111;^\s]+?)\s+(.*?)\s*$#s
# is the delimiter marker telling the regex engine where the regex begins and ends, any character can be used for this.
^ tells the engine to anchor at the start of the line
\s matches any known white-space characters. These include \r\n\t\f<space>
* tells the engine to match zero or more of the term to its left.
() tells the engine to remember the data inside
[] is the character class system. You place characters you wish to look for in here. Each character is allowed to come in any order
^ at the beginning of a character class tells the engine that you don't want the characters inside the class.
\s same as before, white-space.
+ engine will match one or more of the term to its left.
? immediadately following a multiple match like +, *, {} asks the engine to not be greedy. Basically, the engine will try to select the smallest string that fits, instead of the largest.
\s same as before, white-space
+ same as before, one or more.
() same as before, ask engine to remember.
. matches any character, but new-lines (by default)
* same as before, zero or more
? same as before, not greedy.
\s same as before, white-space
* same as before, zero or more
$ end of string anchor.
# closing delimiter, telling the engine to switch to flag reading mode.
s tells the dot (.) metacharacter to match everything, including new-lines.

so putting the whole thing together:
allow any leading spaces, remember all characters up until a space, some number of spaces, remember all characters until trailing spaces at the end of the string.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

:) that goes with my personal tuts. lol
Post Reply