php根据单词截取英文语句,使用php从文本中提取单词

你好朋友有点问题.我只需要提取文本“任何人”的单词.

我尝试使用strtok(),strstr()检索单词.一些正则表达式,但只设法提取一些单词.

由于可以伴随单词的字符和符号的数量,问题是复杂的.

必须提取单词的示例文本.这是一个示例文本:

Main article: our 46,000 required, !but (1947-2011) mail@server.com March 8, 2014 Gutenberg's 34-DE 'a' 3,1415 Us: @unknown n go http://google.com or www.google.com and http://www.google.com (r) The 509th "composite" and; C-54 #dog v4.0 ¿as is done? ¿article... agriculture? x ¿cat? now! Hi!! (87 meters).

Sample text, for testing.

提取文本的结果应该是:

Main article our required but March Gutenberg's a go or and The composite and dog as is done article agriculture cat now Hi meters

Sample text for testing

我写的第一个函数是为了方便工作

function PreText($text){

$text = str_replace("\n", ".", $text);

$text = str_replace("\r", ".", $text);

$text = str_replace("'", "", $text);

$text = str_replace("?", "", $text);

$text = str_replace("¿", "", $text);

$text = str_replace("(", "", $text);

$text = str_replace(")", "", $text);

$text = str_replace('"', "", $text);

$text = str_replace(';', "", $text);

$text = str_replace('!', "", $text);

$text = str_replace('

$text = str_replace('>', "", $text);

$text = str_replace('#', "", $text);

$text = str_replace(",", "", $text);

$text = str_replace(".c", "", $text);

$text = str_replace(".C", "", $text);

return $text;

}

分割功能:

function SplitWords($text){

$words = explode(" ", $text);

$ContWords = count($words);

for ($i = 0; $i < $ContWords; $i++){

if (ctype_alpha($words[$i])) {

$NewText .= $words[$i].", ";

}

}

return $NewText;

}

该程序:

include_once ('functions.php');

$text = "Main article: our 46,000 ...";

$text = PreText($text);

$text = SplitWords($text);

echo $text;

?>

是代码还有很长的路要走.感谢您的帮助.

解决方法:

如果我理解正确,您要删除字符串中的所有非字母.我会用preg_replace

$text = "Main article: our 46,000...";

$text = preg_replace("/[^a-zA-Z' ]/","",$text);

这应该删除所有不是字母,撇号或空格的东西.

标签:php,regex

来源: https://codeday.me/bug/20190609/1206506.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值