Results 1 to 2 of 2
  1. #1
    Senior Member sp00f's Avatar
    Join Date
    Aug 2006

    [PHP Guide] Reading part of a string

    Hello everyone, I am not sure if this have been discussed here before but I will teach you how to read only part of a string in PHP. To do so we will use string function called "substr".

    substr function is used to get part of string. The full syntax of substr is:

    string substr ( string $string , int $start [, int $length ] )
    Now I will explain the parameters of it:

    First parameter is "string $string". With this parameter you choose the string that you want to use this function with. Example:

    $firststring = "This should be the string";
    Second parameter is "int $start". This one is used to tell the function where it should start reading string from. For example, if you enter positive number "1" into this parameter it would start reading the string from the second character(Remember, that in PHP counting starts from 0, not from 1!). So, if we had our string like:

    $firststring = "Test";
    And given number "1" for this parameter it would start reading this string from the letter "e". If you type in negative number just like "-1" for the parameter it would work basically the same, but instead of reading string from its beginning it would read the string from its end. (Notice: If you enter "-0" it just won't work, and would read the whole string)

    The third parameter is "int $length". It is not needed to have this parameter included, but you can use it to tell the function read the string part only in the length you've given.

    So let's put it all together and read five characters from the word: "Hello!".

    $string = "Hello!";
    $printString = substr($string,1,5);
    echo $printString;
    This code would give us respond:

    Hope this will help someone. Took me some time to write it.
    If you found any problem with this please respond.

  2. #2
    Veteran Member SyncX's Avatar
    Join Date
    Feb 2007
    Recife, BR

    Re: [PHP Guide] Reading part of a string

    Lol cool.
    I can't see any problems, but I'm not familiar with PHP...
    But this tut is still great.

    Quote Originally Posted by Book Forward
    Amazing! You’re actually reading this. That puts you into one of three categories: a student who is
    being forced to read this stuff for a class, someone who picked up this book by accident (probably
    because you have yet to be indoctrinated by the world at large), or one of the few who actually have an
    interest in learning assembly language.

Similar Threads

  1. Replies: 2
    Last Post: June 7th, 2010, 13:45
  2. [GUIDE]Complete guide to VAC-Chaos..[/Guide]
    By HOLYSHIT in forum Call of Duty 6: Modern Warfare 2 Cheats
    Replies: 0
    Last Post: April 6th, 2010, 17:59
  3. [Request] Cod SD Noweapon string/bomb plant string for noweapon :P
    By obsidian in forum Call of Duty 1 Cheats
    Replies: 6
    Last Post: August 4th, 2009, 22:22
  4. Best G-String
    By dudewtfhax in forum Comedy
    Replies: 5
    Last Post: July 21st, 2008, 14:10
  5. Smart Html Guide-Part 1 for starts
    By sil#s in forum Html, Php, Xml, Css, ...
    Replies: 7
    Last Post: September 29th, 2007, 12:16


Posting Permissions

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