PHP : How to check if a string contains a specific word/string

in #utopian-io8 years ago (edited)

codeigniter-logo.png

What Will I Learn?

This tutorial is all about how to check if a string contains a specific word or string.

  • How to check if a string contains a specific word.
  • How to use strpos(), preg_match(), substr_count(), and strstr()
  • How to count all the matches found in a string

Requirements

  • PHP Server
  • Basic knowledge about PHP

Difficulty

  • Basic

Tutorial Contents

There are different ways to search a specific word in a string. The best way to do it is by using the strpos() function.

strpos() will find the position of the first occurrence of a substring/word in a string. The position will start at index 0 and if the word does not exist then it will return FALSE.

This is the syntax for strpos():

strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

If third parameter is specified and is positive, the search will start at the beginning of the string until the specified offset. If it is negative then the search will start at the offset until the end of a string.

Example:

<?php

$str = "This is a string.";
echo strpos($str, "string");

?>

This example will search on what position is the word 'string' in the string. And if you run this, it will return 10.

Another way is to use regular expression to check if there is a match using preg_match function. This function will return true if there is a match on a specified word, otherwise false. This is the syntax for preg_match:

preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

Example:

<?php

$str = "This is a string.";
if(preg_match('/\bstring\b/', $str))
    echo 'Matched';

?>

So this example will output 'Matched' because there is a word 'string' in a $str variable.

Another way is to use strstr() function. This function will find the first occurence of the string. The syntax for strstr() is this:

strstr ( string $haystack , mixed $needle [, bool $before_needle = FALSE ] )

Example:

<?php

$str = "This is a string.";
echo strstr($str, "string");

?>

This function will return the portion of a string starting from the searched word until the end. If the searched word is not found, it will return FALSE.

strpos() and strstr() functions are both case-sensitive. So if you want to search a string that should be case-insensitive then you will have to use stripos() and stristr().

Example for stripos():

<?php

$str = "This is a string.";
echo stripos($str, "STRING");

?>

Example for stristr():

<?php

$str = "This is a string.";
echo stristr($str, "STRING");

?>

The last function that we will use is the substr_count(). This will return the count of the occurence of the word in a string. This will return with an integer value. The syntax for substr_count() is this:

substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

Example:

<?php

$str = "This is a string. This is another string.";
echo substr_count($str, "string");

?>

And this example will return 2. Note that this function is a case-sensitive and it will return 0. If you want to use substr_count() with case-insensitive, you can use strtoupper() or strtolower().

Example:

<?php

$str = "This is a string. This is another string.";
echo substr_count(strtoupper($str), "string");

?>

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @gentlemanoi I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.035
BTC 108756.89
ETH 4355.34
USDT 1.00
SBD 0.83