PHP 标题修改

  • 本文关键字:修改 标题 PHP php
  • 更新时间 :
  • 英文 :


我正在开发一个用于 scholar.google.com 的脚本。 脚本需要修改从 http://www.ncbi.nlm.nih.gov/pubmed 获取的文章标题。

因此,脚本必须从标题中删除所有特殊字符、数字和非拉丁字母,以及刚刚提到的单词/符号"触摸"(未与特殊符号、字母或带空格的非拉丁字母分开的单词(。

所以我需要它来转换,例如:

使用 NS1619 激活大电导 Ca2+ 激活的 K+ 通道 降低大鼠逼尿肌平滑肌源性和神经源性收缩 肌肉。

到:

大电导通道激活,肌源性和 大鼠逼尿肌平滑肌的神经源性收缩。

另一个例子是转换它:

小鼠海马中裂链/CX3CL1 介导的 LTP 损伤 通过腺苷受体3型(A3R(的活性

到:

小鼠海马体中的LTP损伤是通过 腺苷受体型活性

我已经拥有的是:

function rename_article ($article){
 global $alphabet;
 $pos_hyphen = strpos($article, "-");
 if ($pos_hyphen===FALSE){
   $article = preg_replace ("/[^a-zA-Z0-9s]/"," ",$article);
  for ($i = 0; $article[$i]; $i++)
  {
   $article .= !is_numeric($article[$i]) ? $article[$i] : "";
  }
  return $article;
 }
 $substr = substr($article, 0, $pos_hyphen);
 $pos1 = strrpos($substr, ' ');
 if ($pos1 === FALSE){
  $pos1 =0;
 }
 $substr2 = substr($article, $pos_hyphen, strlen($article));
 $pos2 = strpos($substr2, ' ');
 if ($pos2 === FALSE){
  $pos2 = strlen($substr2);
 }
 $length1 = $pos_hyphen-$pos1;
 $length2 = $length1+$pos2;
 $substr = substr($article,$pos1,$length2);
 if ($length1<4){
  $article = str_ireplace ($substr, '' ,$article);
  rename_article ($article);
 }
 else{
  foreach ($alphabet as $letter){
   if (strpos($substr, $letter) != FALSE){
    $article = str_ireplace ($substr, '' ,$article);
    rename_article ($article);
   }
   else{
    $article = preg_replace ("/[^a-zA-Z0-9s]/"," ",$article);
    for ($i = 0; $article[$i]; $i++)
    {
     $article .= !is_numeric($article[$i]) ? $article[$i] : "";
    }
    return $article;
   }
  }
 }
 $article = preg_replace ("/[^a-zA-Z0-9s]/"," ",$article);
 for ($i = 0; $article[$i]; $i++)
 {
  $article .= !is_numeric($article[$i]) ? $article[$i] : "";
 }
 return $article;
};

但它并不排除我上面描述的词。

请帮忙

只是给定示例的简单方法:

function rename_article($article) {
    $return = "";
    $array = explode(" ",$article);
    foreach($array as $word) {
        if(preg_match("/^[a-zA-Z.]*$/",$word)) {
            $return.= " ".$word;
        }
    }
    return trim($return);
}
我想

我会和你上面做的有点不同。我会先把标题分解成单词,然后只保留可接受的单词。

function rename_article( $title ) {
    $title = rtrim( $title, ".?!" );  // Added for punctuation at end of title
    $titleWords = explode( ' ', $title );
    $newTitle = '';
    foreach( $titleWords as $titleWord ) {
        if( !preg_match( '/[^a-zA-Z]/', $titleWord ) ) {
            if( empty( $newTitle ) )
                $newTitle = $titleWord;
            else
                $newTitle .= ' ' . $titleWord;
        }
    }
    return $newTitle;
}

先用空格拆分标题,然后按"单词"检查"单词"是否有效或应该删除?

$title = "LTP impairment by fractalkine/CX3CL1 in mouse hippocampus is 
   mediated through the activity of adenosine receptor     type 3 (A3R)";
$title_words = preg_split('/s+/', $title);
$new_title = "";
foreach ($title_words as $word) {
  if (preg_match('/^[a-z]+$/i', $word)) {
     $new_title .= " $word";
  } 
}
$new_title =  trim($new_title);
echo $new_title;

结果

LTP impairment by in mouse hippocampus is mediated through 
the activity of adenosine receptor type

您需要做的第一件事是定义我们要删除的单词的实际模式并将其放入数组中。

显然 + 和 - 在那里,我猜/以及任何带数字的东西? 和 (( 也是。希望我没有错过任何东西。数字字符保证取消资格的事实意味着,如果您愿意,您甚至不必使用一盎司的 RegEx,甚至可以手动将它们粘贴到数组中。

然后,取原句并在空间上爆炸(http://php.net/manual/en/function.explode.php(

然后,散步一下,将值与原始数组进行比较(http://php.net/manual/en/function.array-walk.php(

如果松散数组的值不包含您已经使用过的任何模式,请将其扔到新数组中。

将阵列重新组合在一起 ( http://php.net/manual/en/function.implode.php (

trim(( 并根据需要在末尾粘贴一个句点。

与其使用所有这些正则表达式,我个人只会使用 strpos(( http://php.net/manual/en/function.strpos.php

希望这有帮助。

试试这个

<?php
$string = "Large conductance Ca2+ -activated K+ channel activation
with NS1619 decreases myogenic and neurogenic contractions of rat detrusor smooth muscle.";
$string2 = "LTP impairment by fractalkine/CX3CL1 in mouse hippocampus is
 mediated through the activity of adenosine receptor type 3 (A3R)";

function getTitle($string){
    $s = explode(' ', $string);
    foreach($s as $key => $value){
        if(preg_match('/([a-z]+[0-9+]|[-]+[a-z0-9+])|[0-9]/i', $value)){
           unset($s[$key]);
        }
    }
    return implode(' ', $s);
}
 echo getTitle($string2);
 echo getTitle($string);

输出

LTP impairment by in mouse hippocampus is mediated through the activity of adenosine receptor type

第二

Large conductance channel activation with decreases myogenic and neurogenic contractions of rat detrusor smooth muscle.

最新更新