How to find array values is present in a string of shuffled letters.

$string = "ankegsrdib";
$words = array("ask","bird","egg");

which function should I use to find this.? Also the result should be like this

array("ask"=>"Yes","bird"=>"Yes","egg"="No")

please suggest some ideas.

Recommended Answers

This function tells if characters exist in a string:

http://php.net/manual/en/function.strspn.php

Jump to Post

All 4 Replies

Member Avatar

Not sure how you.d use strspn with the op.s remit. I.d use a loop with strpos and fill the output array in the loop. The array functions used to be really slow so looping was the preferred method. Haven.t checked out the php7 speeds. Perhaps array functions have caught up?

@ diafol, could you please show a sample code for that ?

Member Avatar
$ans = [];
foreach($words as $word)  $ans[$word] = (strpos($string, $word) !== false) ? 'Yes' : 'No';
print_r($ans);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.