简体   繁体   English

PHP匹配特定的句子字母并仅获取单词

[英]PHP match specific letters of sentence and get only the words

I'm tiring to write a PHP code for get matched words by using letters, its not the same strpos. 我很想编写一个PHP代码来使用字母来获取匹配的单词,它不是相同的strpos。

 ex
    $string="How are you robin ?";
    $searchletters="yo";


 in this  "$string" when i search "$searchletters" i need to get 
 the word "you" which have you letters, 

anyone have a idea how to implement this with PHP. 任何人都有一个想法如何使用PHP来实现。 Thank You 谢谢

You can use this regex: 您可以使用此正则表达式:

$re = '/\b' . preg_quote($searchletters, '/') . '\w*\b/';

preg_match($re, $string, $m);
echo $m[0] . "\n";
//=> you

RegEx Demo 正则演示

strtok(strrchr($string,$searchletters), " ");

Explanations: 说明:

strrchr() : finds the position of the last occurrence of a string within another string, and returns all characters from this position to the end of the string. strrchr() :查找一个字符串在另一个字符串中最后一次出现的位置,并返回从该位置到该字符串末尾的所有字符。

strtok() : which can be used to split a string into smaller strings (tokens) based on some separator(s). strtok() :可用于基于某些分隔符将字符串拆分为较小的字符串(令牌)。 For more details and examples, see the strtok PHP manual page 有关更多详细信息和示例,请参见strtok PHP手册页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM