top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

substr() vs strstr() in PHP?

+2 votes
11,178 views

Substr in PHP

Substr means Sub String in english and that's what substr() function used i.e. it returns a part of string. Using substr() function in php we can split a string part by part. This function is present in almost in all language and have the similar syntax and meaning.

Syntax:

substr(string, start, length)

String – This is the input string which we need to split and a mandatory field.
Start – This is the starting Position from which we need to return the substring it can be positive, negative and zero. This is a mandatory field.
Position Values - Starting the String from the beginning.
Negative Values – Starting the String from end.
0 (Zero) – Zero means it will start from first character.
length - It is an optional field and can be positive, negative or zero. If length is not present then substring starting from start until the end of the string will be returned.

Positive Length: the string returned will contain beginning from start till start+length.
Negative Length: the string returned will contain beginning from start till end+length.
Zero Length: Empty string is returned.

SubstrExample

strstr() in PHP

strstr() in php is used to search a string within another string. This is a case sensitive function and is used to search first occurrence string.

Syntax:

Strstr(string, search, before_string);

String: This is mandatory field. This is the input string in which we need to search the search string.
Search: This is mandatory field and a string which needs to be searched.
Before_String: This is optional and default value is false. If we set as true,it will return the string before the first occurrence of string.

Example for strstr without using third parameter
strstr examples

Example for strstr with using third parameter
strstr example

posted Oct 24, 2014 by Madhavi Latha

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...