We help IT Professionals succeed at work.

Solving undefined offset problem

yowwowwow
yowwowwow asked
on
528 Views
Last Modified: 2009-12-16
I am trying to build the auto create session but I ran into a problem with the xml style.
Because of the xml grouping structure which cause the script to failed.
Please pay attention to the <PersonalInfo> tag, this is causing a problem

I'm getting the Undifined Offset  
for line 15 for 16



<?php
if (!isset($_SESSION)) session_start();
 
$AllFields = '
<PersonalInfo>
<Account>43435-1014</Account>
<UserName>TESTME</UserName>
<Email>afa@faf.com</Email>
</PersonalInfo>
';
    
  while ($AllFields != '') {
    $AllFields = trim($AllFields);
    preg_match('/<(.*?)>(.*?)<\/\1>/', $AllFields, $matches);
    $_SESSION[$matches[1]] = $matches[2];
    $AllFields = str_replace($matches[0], '', $AllFields);
  }
  
  echo $_SESSION['Account'];
?>

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
sweet!
CERTIFIED EXPERT

Commented:
It would seem that if you would use a regex pattern such as:
<(?!PersonalInfo)(.*?)>(.*?)<\/\1>

You could create match results such as:
Array
(
    [0] => Array
        (
            [0] => <Account>43435-1014</Account>
            [1] => <UserName>TESTME</UserName>
            [2] => <Email>afa@faf.com</Email>
        )

    [1] => Array
        (
            [0] => Account
            [1] => UserName
            [2] => Email
        )

    [2] => Array
        (
            [0] => 43435-1014
            [1] => TESTME
            [2] => afa@faf.com
        )

)

Effectively ignoring the PersonalInfo tag if that's what you are looking for.  I am only answering the question from a regex standpoint so if I am way off base I apologize.
CERTIFIED EXPERT

Commented:
Thanks for the question and the points.

Author

Commented:
full scripts for future reference

<?php
if (!isset($_SESSION)) session_start();

$AllFields = '
<PersonalInfo>
<Account>43435-1014</Account>
<UserName>TESTME</UserName>
<Email>afa@faf.com</Email>
</PersonalInfo>
';
   
  while ($AllFields != '') {
    $AllFields = trim($AllFields);
    preg_match('/<(.*?)>(.*?)<\/\1>/s', $AllFields, $matches);
    $_SESSION[$matches[1]] = $matches[2];
    $AllFields = str_replace($matches[0], '', $AllFields);
  }
 

    echo $_SESSION['PersonalInfo'];
    echo $_SESSION['Account'];


?>


Author

Commented:
uh you welcome
any way I ran into another problem with the
xml header
line 16 and 17

<?php
if (!isset($_SESSION)) session_start();

$AllFields ="<?xml version='1.0' encoding='utf-8'?>
<UserInfo>
<AccountInfo>
<Account>7434432</Account>
<UserName>TES</UserName>
<Email>DDD@faf.com</Email>
</AccountInfo>
";
   
  while ($AllFields != '') {
    $AllFields = trim($AllFields);
    preg_match('/<(.*?)>(.*?)<\/\1>/s', $AllFields, $matches);
    $_SESSION[$matches[1]] = $matches[2];
    $AllFields = str_replace($matches[0], '', $AllFields);
  }
 

    echo $_SESSION['Email'];

?>


Author

Commented:
undefined offset 1 line 16
undefined offset 0 line 17
CERTIFIED EXPERT

Commented:
For that you might try:
<(?!\?)(.*?)>(.*?)<\/\1>

Although I am not sure why the current pattern would be matching on the <?xml...> declaration, it doesn't seem to match in my testing.

Author

Commented:
if i use the
<(?!\?)(.*?)>(.*?)<\/\1>

Unkown modifier.

Author

Commented:
these regular expression may be good for this script.
Let me seat down and figure it out.

a dollar sign ($) is used to match strings that end with the given pattern.
|a caret (^) character at the beginning of a regular expression indicates that it must match the beginning of the string. The characters that match themselves are called literals. The characters that have special meanings are called metacharacters.
The dot (.) metacharacter matches any single character except newline (\). So, the pattern h.t matches hat, hothit, hut, h7t, etc.
The vertical pipe (|) metacharacter is used for alternatives in a regular expression. It behaves much like a logical OR
The metacharacters +, *, ?, and {} affect the number of times a pattern should be matched.
+ means "Match one or more of the preceding expression",
* means "Match zero or more of the preceding expression", and
? means "Match zero or one of the preceding expression".
Curly braces {} can be used differently. With a single integer, {n} means "match exactly n occurrences of the preceding expression", with one integer and a comma, {n,} means "match n or more occurrences of the preceding expression", and with two comma-separated integers {n,m} means "match the previous character if it occurs at least n times, but no more than m times

Author

Commented:
that's okay, i'll remove the xml line then it should be fine.
CERTIFIED EXPERT

Commented:
Sounds like you will remove the xml line, but here's the pattern in code:
preg_match('/<(?!\?)(.*?)>(.*?)<\/\1>/s', $AllFields, $matches);
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.