AppDividend
Latest Code Tutorials

PHP strstr() Function Example | strstr() Method In PHP

0

PHP strstr() Function Example | strstr() Method In PHP is today’s topic. PHP strstr() function is the built-in function in PHP. PHP strstr() function searches for a 1st occurrence of the string inside another string. The strstr() function is case-sensitive. For a case-insensitive search, you can use the stristr() function. The function returns the rest of a string (from the matching point), or FALSE, if a string to search for is not found inside another string.

PHP strstr() Function

The strstr() function is a binary-safe function, means a function that can perform on binary files without modifying the content of the file.

The syntax of the strstr() function is following.

strstr(string, search, before_search)

The string parameter is required, and it tells the string to search.

The search parameter is required and specifies the string to search for.

The before_search parameter is optional. The boolean value whose default is “false“. If it is set to “true”, it returns a part of the string before a first occurrence of the search parameter.

See the following code.

<?php

// app.php

$op = strstr("Eleven Hopper","Hopper");
echo $op;

See the output.

➜  pro php app.php
Hopper                                                                                  
➜  pro

#Search using ASCII value

Search the string for an ASCII value of “o” and return the rest of the string.

<?php

// app.php

$op = strstr("Hello world!", 111);
echo $op;

See the output.

➜  pro php app.php
o world!                                                                                
➜  pro

Let’s return the part of the string before the first occurrence of “Watson“.

<?php

// app.php

$op = strstr("Emma Watson", "Watson", true);
echo $op;

See the output.

➜  pro php app.php
Emma                                                                                    
➜  pro

The strstr() function is far more memory-intensive than strpos(), especially with a longer string.

So, if you are not interested in the substring that strstr() returns then, you shouldn’t be using it.

The strstr function in PHP is used to return all characters of the string after the first match.

Finally, PHP strstr() Function Example | strstr() Method In PHP is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.