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

All 4 Replies

Member Avatar for diafol

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 for diafol
$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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.