Results 1 to 3 of 3

Thread: explode undefined offset

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    366

    explode undefined offset

    I am getting an undefined offset when using an explode. Usually it works however on one instance it fails. It is the second explode here that is failing, because there was no instance of " v " in the first one:
    Code:
    $pieces = explode(" v ", strip_tags($matches[$gameKeys[$e]]));	
    echo $pieces[1]."<p>";
    $findtime = explode(",", $pieces[1]);
    print_r($findtime);
    produces:
    Tunisia, GpC, 19:00

    Array ( [0] => Tunisia [1] => GpC [2] => 19:00 )


    Notice: Undefined offset: 1 in C:\xampp\htdocs\load_data.php on line 82

    Notice: Undefined offset: 1 in C:\xampp\htdocs\load_data.php on line 83
    Array ( [0] => )

    Crystal Palace, (agg 0-1), SF, L2, 19:45
    Array ( [0] => Crystal Palace [1] => (agg 0-1) [2] => SF [3] => L2 [4] => 19:45 )
    So it works on the one above and below, however one of them seems to have nothing in it, and therefore pops up an error. This won't always be the case, but sometimes this might occur. The data it is reading from shows this:

    Morocco v Tunisia, GpC, 19:00
    Qatar OFF Sweden, 13:00
    Cardiff v Crystal Palace, (agg 0-1), SF, L2, 19:45

    So you can see the Qatar one is of slightly different format - it says OFF instead of " v ". Can i run an if statement on the first one. So if there was a " v " then do the next else have something that will avoid the error occuring?

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: explode undefined offset

    How about replacing the "OFF" with "v" before that ?

    For example:
    PHP Code:
    $t str_replace("OFF" "v"$matches[$gameKeys[$e]]);

    $pieces explode(" v "strip_tags($t));    
    echo 
    $pieces[1]."<p>";
    $findtime explode(","$pieces[1]);
    print_r($findtime); 
    Or, use regular expressions.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: explode undefined offset

    PHP Code:
    $pieces preg_split('/\s+(v|OFF)\s+/'$matches[$gameKeys[$e]]) 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width