1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

[STRING OFFSET] Uninitialized string offset: 0

Discussion in 'Development' started by Infernus101, Dec 6, 2016.

  1. Infernus101

    Infernus101 Witch

    Messages:
    54
    GitHub:
    infernus102
    PHP:
    $words preg_split("/[\s,_-]+/""$message");
    $acronym='';
    foreach (
    $words as $w) {
        
    $acronym .= $w[0];
      if(
    $acronym == '@'){
          
    $mention substr($w1);
          return 
    false;
      }
    }
    Error : Notice: Uninitialized string offset: 0
    Error in line
    PHP:
     $acronym .= $w[0];
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Foreach() splits an array up for each key/value. You have used foreach() here, which means you can't use $w[0] as it is not an array, but a string, being part of an array. you'll either have to use just $w or not use foreach.
     
    applqpak likes this.
  3. Infernus101

    Infernus101 Witch

    Messages:
    54
    GitHub:
    infernus102
    How can you use any other loop for this code i posted?
     
  4. aliuly

    aliuly Silverfish

    Messages:
    23
    GitHub:
    alejandroliu
    Maybe this would work....
    PHP:
    $words preg_split("/[\s,_-]+/""$message");
    $acronym='';
    foreach (
    $words as $w) {
        
    $acronym .= $w{0};
        if(
    $acronym == '@'){
          
    $mention substr($w1);
          return 
    false;
      }
    }
    Note that the "$message" is better off without the quotes.
     
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Sorry, I failed to notice the difference apart from using {} instead of []?

    Also, it would be better to use single quotes for the regex, or escape the backslash.
     
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    Is there any?
     
    applqpak likes this.
  7. archie426

    archie426 Baby Zombie

    Messages:
    130
    GitHub:
    archie426
    Bit like me and my
    Code:
    -o-
    and
    Code:
    -0-
     
  8. aliuly

    aliuly Silverfish

    Messages:
    23
    GitHub:
    alejandroliu
    Yes, but that makes the difference. They want to catch words that begin with "@".

    So doing $w[0], PHP thinks that $w is an array and you want to get the first element in the array.
    Doing $w{0}, PHP thinks that $w is a string and you want to get first character in the string.
     
    HimbeersaftLP and applqpak like this.
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    But aren't they the same for accessing offsets in a string?
    PHP:
    php -'$a =  "abc"; var_dump($a[0]);'
    Command line code:1:
    string(1"a"
     
    applqpak likes this.
  10. aliuly

    aliuly Silverfish

    Messages:
    23
    GitHub:
    alejandroliu
    applqpak likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.