0

For example I have this :

$string = 'PHP is a server side web programming language , PHP is amazing , I love PHP';

$array = array('html','css','javascript','ajax','html5','css3','jquery','PHP');

foreach($array as $ar){
   //Check if one of the $array values exists in the $string
}

I want to search from the end of the $string only , so if the $array value is not at the end then nothing would happen as it's not exist , The PHP could be any of the other values in the $array so I don't know the length the value should be found at , I mean the word could be repeated and with different length.

i.e : $string = 'html .... , html is fantastic , I love html'; , now the length of the word is bigger , and it could be bigger than that.

How to find the last one only what ever the length of the word is ?

Joe
  • 43
  • 7

6 Answers6

1

A regex approach could be:

<?php
$string = 'PHP is a server side web programming language , PHP is amazing , I love PHP';
$array = array('html','css','javascript','ajax','html5','css3','jquery','PHP');
$escaped_array = array_map(function($value){
    return preg_quote($value, '/');
}, $array);
$regex = '/\b(' . implode('|', $escaped_array) . ')\b$/';
preg_match($regex, $string, $match);
print_r($match);

The preg_quote is incase you have special characters in your $array. The \b makes sure it is an exact word match. The | is an "or". Oh, and the $ is so the match is at the end of the string. https://www.regular-expressions.info/anchors.html

Demo: https://3v4l.org/8AYvu

however a regex is not needed here:

$string = 'PHP is a server side web programming language , PHP is amazing , I love PHP';
$array = array('html','css','javascript','ajax','html5','css3','jquery','PHP');
$words = explode(' ', $string);
if(in_array(end($words), $array)){
    echo 'Matched';
}

Demo: https://3v4l.org/0LD5i

chris85
  • 23,846
  • 7
  • 34
  • 51
1

A really simple and efficient way of achieving this is to use substr() and strrpos() to extract the last word from the string.

Then you can search if the $last_word is existing in $array with the in_array() function.

$last_word = substr($string, strrpos($string, ' ') + 1);

if (in_array($last_word, $array)) {
    // OK
}
Cédric
  • 467
  • 2
  • 9
1

You can check a substring the same length as the search term at the end of the string you're searching and see if they match.

foreach ($array as $ar) {
    // negative number in substr will take a substring that many characters from the end
    if ($ar == substr($string, -strlen($ar))) {
        // found it.
        break;
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

Try using regex's closing anchor $ like this:

$string = 'PHP is a server side web programming language , PHP is amazing , I love PHP';

$array = ['html', 'css', 'javascript', 'ajax', 'html5', 'css3', 'jquery', 'PHP'];

foreach ($array as $ar) {
    // Check if one of the $array values exists in the $string
    if (preg_match("/$ar$/", $string)) {
        echo "Found $ar at the end of \$string";
        break;
    }
}

This outputs:

Found PHP at the end of $string

eval.in demo

Ethan
  • 4,295
  • 4
  • 25
  • 44
  • What if the "PHP" is not exist at the end but somewhere else , What would happen ? – Joe Oct 25 '17 at 16:21
  • @Joe It wouldn't find it. You can try forking the eval.in demo and messing around with `$string` in there. – Ethan Oct 25 '17 at 16:23
0

you can use strpos

foreach($array as $ar){
   if(strpos($ar,$string) !== false){
     // $ar exist in string 
   }
}
0

If you can be sure the $string always contains human readable contents, and therefore being separated by spaces, you could start by exploding the string on the spaces, and then check the last element of the resulting array for existence in your word list.

So something like this should work out:

<?php
    $elements = explode(" ", $string);
    $suspect = end($elements);
    if(in_array($suspect, $array)){
        echo "The string '".$suspect."' was found in the string.";
    }
?>
Thomas
  • 76
  • 8