如何从句子末尾删除几个单词?

时间:2015-04-03 14:09:16

标签: php

我有类似的东西:

" YAKUGAKU ZASSHI-JAPAN日本医药学会期刊 YAKUGAKU ZASSHI "

并希望删除第二个" YAKUGAKU ZASSHI" (以粗体显示)来自它。我该怎么办?

我有一个列表,在每一行中,在第一行和最后重复一些事情,我想删除最后重复的那些。我该怎么办?

更新:

我找到了解决方案,所以只是想更新我的问题以防有人遇到同样的问题。

所以实际上我试图从http://images.webofknowledge.com/WOK46/help/WOS/Y_abrvjt.html

获取期刊及其缩写的列表

然后将它们显示为期刊名称:缩写

但我从网站得到的是因为定义列表DT和DD标签没有关闭。

所以实际上我有2个字符串,$ dd和$ dt。在$ dt重复$ dd.so这就是我删除副本所做的:

 $length=strlen($dd); // get length of the dd
 $ddN=explode(' ', $dd); // split the dd and put in new variable called $ddN
 $extra=$ddN[0];  // get the first word of dd
 $pos=strripos($dt,$extra); // find the position of the last occurrence of $extra in $dt, which is first word of $dd
 $result=substr($dt,$pos,-$length); //removing the duplicate from end of the dd
 echo '"' . $result . '"' . '  :  ' . '"' . $dd . '"' . '<br>';

3 个答案:

答案 0 :(得分:0)

如果你能吃正则表达式,这里很小solution

$textWithoutLastWord = preg_replace('/\W\w+\s*(\W*)$/', '$1', $words);

或使用功能:

$lastSpacePosition = strrpos($text, ' ');
$textWithoutLastWord = substr($text, 0, $lastSpacePosition);

答案 1 :(得分:0)

据我所知 - 你想避免在标题(句子)中重复的单词。 这可以通过以下方式实现:

<?php

//Our input string - can be from database, or whatever, trimmed from ending/trailing spaces
$string = trim("YAKUGAKU ZASSHI-JOURNAL OF THE PHARMACEUTICAL SOCIETY OF JAPAN YAKUGAKU ZASSHI");
//Convert title to array of words
$array = explode (" ", $string);
//How many elements we have in array
$count = count($array); 
//Remove the last 2
unset($array[$count-1]);
unset($array[$count-2]);
print_r ($array);
?>

选中fiddle

答案 2 :(得分:0)

我找到了解决方案,所以只是想更新我的问题以防有人遇到同样的问题。

所以实际上我试图从http://images.webofknowledge.com/WOK46/help/WOS/Y_abrvjt.html

获取期刊及其缩写的列表

然后将它们显示为期刊名称:缩写

但我从网站得到的是因为定义列表DT和DD标签没有关闭。

所以实际上我有2个字符串,$ dd和$ dt。在$ dt重复$ dd.so这就是我删除副本所做的:

       $length=strlen($dd); // get length of the dd
       $ddN=explode(' ', $dd); // split the dd and put in new variable called $ddN
       $extra=$ddN[0];  // get the first word of dd
       $pos=strripos($dt,$extra); // find the position of the last occurrence of $extra in $dt, which is first word of $dd
       $result=substr($dt,$pos,-$length); //removing the duplicate from end of the dd
       echo '"' . $result . '"' . '  :  ' . '"' . $dd . '"' . '<br>';
相关问题