PHP string contains
PHP Tutorial » PHP string contains
What is a string contains?
A string is a sequence of characters, as: "Learn PHP with agernic.com!".
How to Check If a String Contains a Specific Word in PHP? To get this you can use afunction named strpos() to check if a string contains a specific word or not. PHP strpos() function returns the position of the first substring in a string
Let's have an example to understand how strpos() works:
<?php
$my_word = "track";
$my_string = "Aspiring champion race car Lightning McQueen
is on the fast track to success, fame, and everything he's ever hoped for";
// Test if string contains the word
if(strpos($my_string, $my_word) !== false){
echo "Word Found in string!";
} else{
echo "Word Not Found in string!";
}
?>
PHP string contains multiple words
Simply, using preg_match(), you can add many different words in the pattern, just use this separator " | " (without quotes) between words.
<?php
$my_string = "Aspiring champion race car Lightning McQueen
is on the fast track to success, fame, and everything he's ever hoped for";
if (preg_match("/race|fame|ever/", $my_string))
{
echo "words was matched";
}
else echo "not fund";
?>
Related subjects:
What is an array
Convert array to string comma
Array to comma separated
Tags: php string contains: substring, specific words, case insensitive, character, php 7, multiple words, array, regex, number php string start with,
How do you check if a string contains a character PHP?
<?php
$my_string = 'btt hi no : foo{[';
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $my_string)) {
// one or more of the 'special characters' found in $string
echo "characters was matched";
}
else echo "characters not fund";?>
PHP strpos() Function
strpos() function finds the position of the first occurrence of a string inside another string
<?php
echo strpos("What is best description for PHP Preg_match () function?
!","for");
?>
php string contains: substring, specific words, case insensitive, character, php 7, multiple words, array, regex, number php string start with, How do you check if a string contains a substring?
PHP string contains - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.