Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search text using php if ($text contains "World")

Tags:

How to search text using php?

Something like:

<?php  $text = "Hello World!";  if ($text contains "World") {     echo "True"; }  ?> 

Except replacing if ($text contains "World") { with a working condition.

like image 562
faressoft Avatar asked Oct 16 '10 20:10

faressoft


People also ask

How will you locate a string within a string in PHP?

The strpos() function finds the position of the first occurrence of a string inside another string. Note: The strpos() function is case-sensitive. Note: This function is binary-safe.

How do you check if a string contains a word in PHP?

You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .

How do I search for a word in PHP text?

PHP's strstr() function simply takes a string to search, and a chunk of text to search for. If the text was found, it returns the portion of the string from the first character of the match up to the end of the string: $myString = 'Hello, there!'; echo strstr( $myString, 'llo' ); // Displays "llo, there!"

How do you find if an array contains a specific string in PHP?

Approach: In order to search an array for a specific value, we will be using the in_array() function where the parameter for the search is of string type & its value is set to true. Otherwise, this function returns a false value if the specified value is not found in an array.


1 Answers

In your case you can just use strpos(), or stripos() for case insensitive search:

if (stripos($text, "world") !== false) {     echo "True"; } 
like image 118
BoltClock Avatar answered Sep 19 '22 13:09

BoltClock