What is strstr () in PHP?

Return Value:Returns the portion of string, or FALSE if the string to search for is not found.Changelog:Since PHP 7.3.0, passing an integer as search parameter has been deprecated.Version:PHP 4+


Syntax

The basic syntax of the strstr() function is given with:

strstr(string, search, before_search);

The following example shows the strstr() function in action.

Tip: If you simply want to find out if a particular substring occurs within a string or not, use the faster and less memory intensive function strpos() instead.


Parameters

The strstr() function accepts the following parameters.

ParameterDescriptionstringRequired. Specifies the string to search.searchRequired. Specifies the string to search for.before_searchOptional. If set to true, it returns the part of the string before the first occurrence of the search string. Default value is

0 which returns all of the string after the first occurrence of the search string (including search string itself).

  • Home
  • PHP Home
  • PHP function Reference
  • ▼String
  • addcslashes
  • addslashes
  • bin2hex
  • chop
  • chr
  • chunk_split
  • convert_cyr_string
  • convert_uudecode
  • convert_uuencode
  • count_chars
  • crc32
  • crypt
  • explode
  • fprintf
  • get_html_ translation_ table
  • hebrev
  • html_entity_decode
  • htmlentities
  • htmlspecialchars_ decode
  • htmlspecialchars
  • implode
  • join
  • ltrim
  • md5_file
  • md5
  • metaphone
  • nl2br
  • number_format
  • ord
  • parse_str
  • print
  • printf
  • quoted_printable_ decode
  • quotemeta
  • rtrim
  • sha1_file
  • sha1
  • similar_text
  • sprintf
  • sscanf
  • str_repeat
  • str_shuffle
  • str_split
  • str_word_count
  • strcasecmp
  • strchr
  • strcmp
  • strcoll
  • strcspn
  • strip_tags
  • stripcslashes
  • stripos
  • strlen
  • strnatcmp
  • strncasecmp
  • strncmp
  • strrchr
  • strrev
  • strripos
  • strrpos
  • strspn
  • strstr
  • strtolower
  • strtoupper
  • strtr
  • substr_compare
  • substr_count
  • substr_replace
  • substr
  • trim
  • ucfirst
  • ucwords
  • vfprintf
  • vprintf
  • wordwrap

PHP: strstr() functionLast update on August 19 2022 21:50:39 (UTC/GMT +8 hours)

Description

The strstr() function is used to get the first occurrence of a string inside another string.

This function is case-sensitive.

Version:

(PHP 4 and above)

Syntax:

strstr(string_name, search_string, before_search) 

Parameters:

NameDescriptionRequired /
OptionalTypestring_nameThe input string.RequiredStringsearch_stringThe string to search for.RequiredMixed*before_searchReturns the part of the string_name before the first occurrence of the search_string when true.OptionalBoolean

*Mixed: Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value:

The portion of the string, or false if search_string is not found.

Value Type: String.

Pictorial Presentation

What is strstr () in PHP?

Example:

<?php
$string1="W3RESOURCE.COM";
$newstring=strstr($string1,".");
echo $newstring;
?>

Output:

.COM

View the example in the browser

See also

PHP Function Reference

Previous: strspn
Next: strtolower



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP - Using braces with dynamic variable names in PHP

Wrap them in {}:

${"file" . $i} = file($filelist[$i]);

Working Example

Using ${} is a way to create dynamic variables, simple example:

${'a' . 'b'} = 'hello there';
echo $ab; // hello there

Ref : https://bit.ly/2XPoYGJ

 



  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises


What is the function of strstr in PHP?

The strstr() function searches for the first occurrence of a string inside another string. Note: This function is binary-safe. Note: This function is case-sensitive. For a case-insensitive search, use stristr() function.

What is difference between strstr () and Stristr () in PHP?

The stristr() is a case-insensitive function which is similar to the strstr(). Both functions are used to search a string inside another string. The only difference between them is that stristr() is case-insensitive whereas strstr() is case-sensitive function.

What is the difference between strstr () and Stristr () How is it possible to remove escape characters from a string?

Difference between stristr() and strstr() functions in PHP Strings can be searched and modified. Both of these PHP functions strstr() and stristr() are used to search a string inside another string. strstr() Method: The strstr() method is used to search the string inside another string in a case-insensitive manner.

How to check if a string contains a substring in PHP?

To check if a PHP string contains a substring, you can use the strpos($string, $substring) function. If the string contains the substring, strpos() will return the index of the first occurrence of the substring in the string and "false" otherwise. In PHP, indexes start at 0.