Mani
Mani

Reputation: 155

substr function returning wrong result in php

I am a VB.NET developer working on a PHP project, I am trying to convert my VB.NET function into php, but not getting the desired result. After debugging I found a problem with substr function.

in VB.NET I am using

mid(105,2,1) 
This giving me output 0

but in php

substr(105,2,1)
Giving me output 5

Upvotes: 1

Views: 1735

Answers (2)

daxeh
daxeh

Reputation: 1085

substr() takes in 2 arguments and an optional third argument

( string, start position, *result length )

*note: result length

  • if +ve then offset from start of returned results,
  • if -ve then offset from end of returned results

example:

   substr("abcde",2,1);

   // string: "abcde"
   // start position: 2  => returns "cde"
   // result length: 1  => returns "c"


   substr(105,2,1);

   // string: "105"
   // start position: 2  => returns "5"
   // result length: 1  => returns "5"

You can test substr online here. Hope this helps.

Upvotes: 1

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

If you want get the 0 in PHP your code should be:

echo substr(105,1,1);

Output:

0

If you want know why, read this:

mid function in VB.NET

Public Shared Function Mid( _
   ByVal str As String, _
   ByVal Start As Integer, _
   Optional ByVal Length As Integer _
) As String

str - Required. String expression from which characters are returned.

Start - Required. Integer expression. Starting position of the characters to return. If Start is greater than the number of characters in str, the Mid function returns a zero-length string (""). Start is one based.

Length Optional. Integer expression. Number of characters to return. If omitted or if there are fewer than Length characters in the text (including the character at position Start), all characters from the start position to the end of the string are returned.

Example

' Creates text string. 
Dim TestString As String = "Mid Function Demo" 
' Returns "Mid". 
Dim FirstWord As String = Mid(TestString, 1, 3)
' Returns "Demo". 
Dim LastWord As String = Mid(TestString, 14, 4)
' Returns "Function Demo". 
Dim MidWords As String = Mid(TestString, 5)

substr function in PHP

string substr ( string $string , int $start [, int $length ] )

string - The input string. Must be one character or longer.

start - If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

If string is less than or equal to start characters long, FALSE will be returned.

Example

<?php
echo substr('abcdef', 1);     // bcdef
echo substr('abcdef', 1, 3);  // bcd
echo substr('abcdef', 0, 4);  // abcd
echo substr('abcdef', 0, 8);  // abcdef
echo substr('abcdef', -1, 1); // f
?>

Read more at:

https://msdn.microsoft.com/en-us/library/05e63829%28v=vs.90%29.aspx

http://php.net/manual/en/function.substr.php

Upvotes: 3

Related Questions