AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP string contains


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

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.

Example (this is HTML - PHP editor, change text on this window)

<?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?

Example (this is HTML - PHP editor, change text on this window)

<?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

Example (this is HTML - PHP editor, change text on this window)

<?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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...