So basically, I am simply trying to search for a string within a string.
Although, it is a little trickier than that.
I have three small strings, one
, two
, three
And I have one large string, which is saved as a variable and is quite long.. A few paragraphs long actually.
I need to create a function that allows me to see which string occurs first.
For example, a string like this:
Hello, testing testing one test some more two
Will return one
since it occurred before two
.
Another example:
Test a paragraph three and testing some more one test test two
Will return three
since it occurred before both one
and two
.
Does anyone have any suggestions or examples on how to do this? Very new to PHP and not to sure how to go about doing this. Thanks!
Try something like this:
$string = 'Hello, testing testing one test some more two';
$words = Array("one", "two", "three");
$low_pos = strlen($string);
$first = '';
foreach($words as $word)
{
$pos = strpos($string, $word);
echo "Found ".$word." at ".$pos."<br />";
if($pos !== false && $pos < $low_pos)
{
$low_pos = $pos;
$first = $word;
}
}
echo $string."<br />";
echo "FIRST: ".$first;
Output:
Found one at 23
Found two at 42
Found three at
Hello, testing testing one test some more two
FIRST: one
See http://www.php.net/manual/en/function.strpos.php
Simply something like
$longString = "Hello, testing testing one test some more two";
$onePosition = strpos($longString, "one");
$twoPosition = strpos($longString, "two");
$threePosition = strpos($longString, "three");
$onePosition; // 23
$twoPosition; // 42
$threePosition; // -1
Then you would just compare each variable to find the lowest. Clunky but for 3 variables not much work.
This should work:
<?php
$arrWords = array("one", "two", "three");
$strInput = "Test a paragraph three and testing some more one test test two";
function getFirstOccurrence($strInput, $arrWords) {
$arrInput = explode(" ", $strInput);
foreach($arrInput as $strInput) {
if(in_array($strInput, $arrWords)) {
return $strInput;
}
}
return null;
}
print "First word is: " . getFirstOccurrence($strInput, $arrWords);
?>
Here's a sample algorithm:
function getFirstWord(array $needles, $haystack) {
$best = null; //the best position
$first = null; //the first word
foreach($needles as $needle) {
$pos = strpos($haystack, $needle);
if($pos !== false) {
if($best === null || $pos < $best) {
$best = $pos;
$first = $needle;
}
}
}
//returns the first word, or null if none of $needles found
return $first;
}
$needles = array('one', 'two', 'three');
echo getFirstWord($needles, 'Hello, testing testing one test some more two'); // one
echo getFirstWord($needles, 'Test a paragraph three and testing some more one test test two'); // three
An optimal solution would minimise iterations over $haystack. You could start at the beginning of the string, and each time you advance a character, look for any of the $needles starting at the current position. As soon as you find one, bingo.
Use preg_match()
with a simple alternation:
if (preg_match('/one|two|three/', $string, $matches)) {
echo $matches[0];
}
Results:
Hello, testing testing one test some more two
-> one
Test a paragraph three and testing some more one test test two
-> three
If you need the position as well, you can add a flag:
if (preg_match('/one|two|three/', $string, $matches, PREG_OFFSET_CAPTURE)) {
echo 'Found ' . $matches[0][0] . ' @ ' . $matches[0][1];
}
As a function:
function findFirst($string, array $needles)
{
$pattern = '/' . join('|', array_map(function($str) {
return preg_quote($str, '/');
}, $needles)) . '/';
if (preg_match($pattern, $string, $matches)) {
return $matches[0];
} else {
return false;
}
}
To use:
echo findFirst($string, array('one', 'two', 'three'));
If you want to get fancy you can use a closure with array_map, array_filter, array_search and min.
function whichFirst(array $list, $string){
// get a list of the positions of each word
$positions = array_map(function($val) use ($string){
return strpos($string, $val);
}, $list);
// remove all of the unfound words (where strpos returns false)
$positions = array_filter($positions, function ($x){ return $x !== false; });
// get the value with the key matching the lowest position.
$key = array_search(min($positions), $positions);
return $list[$key];
}
Example:
$str = "Hello, testing testing one test some more two";
$list = ["one","two","three"];
echo whichFirst($list, $str);
// outputs one