How can a check the first and last character in a string.

The charater in between could be anything.

Thanks

    $string = "HELLO";
    echo $string[0];
    echo $string[-1];

    EDIT: Oh figs...that only works in Python. I'll try and find the solution for you.

      Wait no that doesn't work!

      I can't find the solution on php.net either.

      Stoopid me...

      EDIT: Found a way! Complicated but a way none the less!

      <?PHP
      
      $string = "Hello";
      
      function unichar($string) {
      $two= strtolower(str_replace(' ', '', $string));
      $res = count(count_chars($two, 1));
      return $res;
      }
      
      $number = unichar($var);
      $num = $number - 1;
      
      echo $string[0];
      echo $string[$num];
      
      ?>
      
      

      EDIT2: Okay that only returns the first character, not the last. Okay I'm all out of ideas.

        Just $str{0} for first character, $str{strlen($str)-1} for last character.

          Let's turn him on to "substr()". He'll probably discover it eventually anyway when he gets around to finding the section of the manual devoted to string functions.

          // First character:
          substr($str, 0, 1);
          // Last character:
          substr($str, -1);

            Haha sh*t! I couldn't find that bit for some strange reason..!!

              Write a Reply...