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 ...
source share