Find the part of the string and print the entire string

I would like to find part of the string, and if true , I want to print the entire string found.

The following is an example:

 $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; if(strstr($Find, $Towns)){ echo outputWholeString($Find, $Towns); // Result: Eccleston. } 

If someone can shed light on how to do this, and do not forget that these will not be static values; $Towns and $Find variables will be dynamically assigned in my live script.

+6
source share
6 answers

Use explode() and strpos() as

 $Towns = "Eccleston, Aberdeen, Glasgow"; $data=explode(",",$Towns);// $Find = "Eccle"; foreach ($data as $town) if (strpos($town, $Find) !== false) { echo $town; } 

Demo

+5
source

You were there ...

This is probably what you are looking for:

 <?php $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; if(stripos($Towns, $Find)) { echo $Towns; } 

Conclusion: Eccleston, Aberdeen, Glasgow , which I would call "the whole line."


If you want to output only the partially matched part of the "whole line", look at this example:

 <?php $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; foreach (explode(',', $Towns) as $Town) { if(stripos($Town, $Find)) { echo trim($Town); } } 

The result of this is obviously equal: Eccleston ...

Two general points:

  • the strpos() / stripos() functions are better suited here, since they only return the position instead of the whole string, which is enough for this purpose.

  • using stripos() instead of strpos() does a case-insensitive search, which probably makes sense for the task ...

+1
source

Using preg_match() , you can search for Eccle and return the word Eccleston .

I am using Pearl Compatible Regular Expression (PCRE) '#\w*' . $Find . '\w*#' '#\w*' . $Find . '\w*#' '#\w*' . $Find . '\w*#' in the code below and demo .

The # characters are PCRE delimiters. A search pattern is inside these delimiters. Some people prefer / as a separator.
\w indicates the characters of the word.
* indicates 0 or more repetitions of the previous character.
So, #\w*Eccle\w*# PCRE looks for a string containing an Eccle surrounded by one or more characters of the word (letter)

 <?php $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; if (preg_match('#\w*' . $Find . '\w*#', $Towns, $matches)) { print_r($matches[0]); } ?> 

Launch Code: http://sandbox.onlinephpfunctions.com/code/4e4026cbbd93deaf8fef0365a7bc6cf6eacc2014

Note: '#\w*' . $Find . '\w*#' '#\w*' . $Find . '\w*#' '#\w*' . $Find . '\w*#' matches "#\w*$Find\w*#" (note the individual single or double quotes). Cm. .

+1
source

You should use strpos () to search for a string inside another:

 if( strpos($Towns, $Find) === false ) { echo $Towns; } 

Note that you need to use "===" to find out if strpos () returns false or 0.

+1
source

Solution using the preg_match function:

 $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; preg_match("/\b\w*?$Find\w*?\b/", $Towns, $match); $result = (!empty($match))? $match[0] : ""; print_r($result); // "Eccleston" 
+1
source

Assuming you will always have $ Towns separated by "," then you can do something like this

 $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; $TownsArray = explode(", ", $Towns); foreach($TownsArray as $Town) { if(stristr($Find, $Town)) { echo $Town; break; } } 

The above code will output the City as soon as it finds the needle and exits the foreach loop. You can remove "break;" to continue running the script to see if it finds more results.

+1
source

All Articles