


PHP string exercise 4: Determine whether a string contains a specific word
In "PHP String Exercise 3: 4 Methods for Converting String Sizes", we introduced four common methods for converting the case of strings. In this article, we will continue with strings. The series determines whether a string contains a specific string.
In fact, there is a very simple way to achieve such a judgment. Let’s look at the code directly:
As follows:
<?php $str1 = 'The quick brown fox jumps over the lazy dog.'; if (strpos($str1,'jumps') !== false) { echo '特定词出现了'; } else { echo '特定词未出现'; }
In this code, we create a String variable $str1, then we randomly specify the word "jumps" and check to see if this word exists in $str1 through the if...else statement;
Let's take a look at the judgment results first:
As shown in the picture, the result is that "a specific word appears". From the above code, it is indeed true.
So here we mainly use a function strpos and the operator "!==".
The built-in function strpos() in PHP is used to find the first occurrence of a string in another string. Its syntax is "strpos(string,find,start)
", where the parameter string specifies the string to be searched, find specifies the string to find, and start is optional and specifies where to start the search; this function returns the position of the first occurrence of the string in another string , returns FALSE if the string is not found.
It should be noted that the strpos() function is case-sensitive.
!==
means "absolutely not equal" in comparison operators, !==
means "not equal" in array operators .
Finally, I recommend reading "php detects whether a string contains a string". This article introduces other methods of detecting inclusion. Friends who are interested can learn about it~
Recommend the classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn!
The above is the detailed content of PHP string exercise 4: Determine whether a string contains a specific word. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In PHP, you can use the ord() function to convert characters into ascii code. This function can return the ASCII value of a single character or the first character in a string. The returned ASCII value will be displayed in integer form; the conversion syntax "ord (string)", the parameter "string" cannot be omitted, it is the string (or single character) from which the ASCII value is to be obtained.

There are two ways to replace a certain character with a null character in a PHP string: 1. Use the str_replace() function to replace the specified character with a null character. You only need to set the first parameter to the specified character and the second parameter to a null character. Syntax "str_replace("specified character","", $str)"; 2. Use the preg_replace() function with regular expressions to match the specified character and replace it with the null character, syntax "preg_replace('/specified character/', "",$str)".

Two removal methods: 1. Use preg_replace() to execute a regular expression to search for all uppercase letters and replace them with null characters. The syntax is "preg_replace('/[A-Z]/','',$str)". 2. Use preg_filter() to execute a regular expression to search for all uppercase letters and replace them with empty characters. The syntax is "preg_filter('/[A-Z]/','',$str)".

PHP is a typed programming language that is often used to develop web applications. During web development, you may need to perform various operations on strings, such as removing specific characters from a string, retaining numbers or letters in a string, etc. In this article, we will focus on how to remove specific characters on the left or right side of a string in PHP.

Two methods: 1. Use preg_match_all() with regular filter strings, the syntax is "preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);" ; 2. Use preg_replace() with regular search for non-Chinese letters in the string and replace them with empty characters. The syntax is "preg_replace("/[^\x{4E00}-\x{9FFF}]+/u" ,'',$str)".

PHP is a very popular programming language and one of the preferred tools for building dynamic websites. In PHP development, we often need to operate strings, and one common requirement is to remove double quotes from strings. In this article, we will introduce some methods to remove double quotes from PHP strings.

PHP can add characters to strings. Two implementation methods: 1. Use the string connector "." to splice the specified character to the beginning or end of the string. The syntax is "specified character. string" or "string. specified character"; 2. use substr_replace The () function can insert the specified character at the specified position of the string. The syntax is "substr_replace(string, specified character, specified position, 0)". The value at the specified position can be 0, negative or positive.

PHP is a commonly used server-side scripting language that is widely used in web development. PHP's processing of strings is quite flexible, making it easy to convert strings into date and time format. This article will introduce how to use PHP to convert a string into date and time format.
