5

I would like to find part of a string and if true I want to ouput the whole of the string that it finds.

Below is an example:

$Towns = "Eccleston, Aberdeen, Glasgow";
$Find = "Eccle";

if(strstr($Find, $Towns)){
    echo outputWholeString($Find, $Towns); // Result: Eccleston.
}

If anyone can shed some light on how to do this as well, and bare in mind that it will not be static values; the $Towns and $Find variables will be dynamically assigned on my live script.

Connor Simpson
  • 487
  • 1
  • 7
  • 27
  • The syntax is `strstr($haystack, $needle)`, so you probably mean `strstr($Towns, $Find)` instead of `strstr($Find, $Towns)` – PaulH Jun 29 '16 at 14:04

6 Answers6

4

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

Saty
  • 22,443
  • 7
  • 33
  • 51
  • With `explode()`, the string is converted to an array and the array is searched item per item. The `preg_match()` solution does not convert to array but the search is more complex. – PaulH Jun 29 '16 at 14:10
0

You were nearly there...

This is probably what you are looking for:

<?php
$Towns = "Eccleston, Aberdeen, Glasgow";
$Find = "Eccle";

if(stripos($Towns, $Find)) {
    echo $Towns;
}

The output is: Eccleston, Aberdeen, Glasgow which is what I would call "the whole string".


If however you only want to output that partly matched part of "the whole string", then take a look at that example:

<?php
$Towns = "Eccleston, Aberdeen, Glasgow";
$Find = "Eccle";

foreach (explode(',', $Towns) as $Town) {
    if(stripos($Town, $Find)) {
        echo trim($Town);
    }
}

The output of that obviously is: Eccleston...

Two general remarks:

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

  2. the usage of stripos() instead of strpos() performs a case insensitive search, which probably makes sense for the task...

arkascha
  • 41,620
  • 7
  • 58
  • 90
0

Using preg_match(), it is possible to search for Eccle and return the Eccleston word.

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

The # characters are PCRE delimiters. The pattern searched is inside these delimiters. Some people prefer / as delimiter.
The \w indicates word characters.
The * indicates 0 or more repetions of the previous character.
So, the #\w*Eccle\w*# PCRE searches for an string containing Eccle surrounded by one or more word characters (letters)

<?php
$Towns = "Eccleston, Aberdeen, Glasgow";
$Find = "Eccle";

if (preg_match('#\w*' . $Find . '\w*#', $Towns, $matches)) {
    print_r($matches[0]);
}
?>

Running code: http://sandbox.onlinephpfunctions.com/code/4e4026cbbd93deaf8fef0365a7bc6cf6eacc2014

Note: '#\w*' . $Find . '\w*#' is the same as "#\w*$Find\w*#" (note the surrounding single or double quotes). See this.

Community
  • 1
  • 1
PaulH
  • 2,918
  • 2
  • 15
  • 31
0

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

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

Note that you have to use "===" to know if strpos() returned false or 0.

Diego Bauleo
  • 681
  • 3
  • 12
0

The solution using 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"
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

Assuming that you will always have $Towns separated by ", " then you could 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 Town once it finds the needle and exit the foreach loop. You could remove the "break;" to continue letting the script run to see if it finds more results.

Kuku Bhai
  • 11
  • 6