3

我找到了与我正在寻找的答案相似的答案,但不是完全解决我的问题的答案。

用户单击我页面上的链接,该链接将他们带到 Google 图书并进行搜索。但是,如果书名中的第一个单词包含缩略语,那么一切都会被抛弃,我的更正似乎都不起作用。

示例:我页面上的链接如下所示:

mysite.com/book-search?bookauthor=Lance Armstrong&booktitle=It's Not About the Bike

但是,当我运行这个时:

$bookTitle = $_GET['booktitle']

我得到了相同的标题,但删除了“It's”中的撇号。这足以完全摆脱 Google 图书搜索并且不返回任何结果。

执行搜索的 cURL 会话中的端点如下所示:

https://www.googleapis.com/books/v1/volumes?q=intitle:' . $bookTitle . '+inauthor:' . $bookAuthor;

自己试试——

https://www.googleapis.com/books/v1/volumes?q=intitle:It%27s+Not_About+The+Bike+inauthor:Lance+Armstrong

返回完全不同的结果

https://www.googleapis.com/books/v1/volumes?q=intitle:Its+Not_About+The+Bike+inauthor:Lance+Armstrong

你会认为,如果我对字符串进行 urlencoded,就可以解决问题。但事实并非如此。尽管如此,由于我无法理解的原因,当我这样做时:

$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=intitle:' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);

...当将 urlencoding 传递给 GoogleAPI 时,它会被剥离,我再次得到零结果搜索。

有人可以帮我解决这个问题吗?我已经尝试了我能想到的一切。

哦 - 这是奖励- 只有当字符串中的第一个单词包含撇号时才会发生这种情况。所以它发生在像“这不是关于自行车”和“不要眨眼”这样的书名上。换句话说,撇号似乎并不重要。(例如,作者比尔·奥莱利(Bill O'Reilly)。)

如果字符串中包含撇号,是否有一种简单的方法可以从字符串中删除第一个单词?或者,还有更好的方法?

真正丑陋的解决方案:

所以 PHP 通过 urldecode() 传递 $_GET 变量。因此,即使它很丑陋,这就是我最终解决问题的方法,尽管我希望有人会想出更好的方法。

在通过 URL 传递变量之前,我将撇号替换为一串乱码,如下所示:

$booktitle = str_replace("'", "W5x32RS97", $booktitle);

然后运行这个:

$bookTitleTemp = $_GET('booktitle');
$bookTitle = str_replace("W5x32RS97", "'", $bookTitleTemp);

突然间,一切正常。但是必须有更好的方法。

4

5 回答 5

1

您没有尝试在标题字符串中替换 'by \' 吗?我的意思是,而不是:这不是关于自行车,使用这个:这不是关于自行车

于 2012-10-10T15:30:05.007 回答
1

如果没有撇号,您可以删除intitle:并仅使用以下字符串:

$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);

https://www.googleapis.com/books/v1/volumes?q=Its+Not_About+The+Bike&inauthor:Lance+Armstrong

更新

如果第一个单词有撇号,要编写要使用的条件intitle,您可以执行以下操作:

$pieces = explode(" ", $bookTitle);

if (strpos($pieces[0], "'") !== FALSE) {
    $google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
}
else {
    $google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=intitle:' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
}

我还想看看之前对变量进行编码是否可行:

$bookTitle = urlencode($_GET[booktitle]);
于 2012-10-10T15:36:35.870 回答
0

绝望的疾病需要绝望的治疗。

$translation = array("aint" => "ain't", "arent" => "aren't", "cant" => "can't", "cantve" => "can't've", "cause" => "'cause", "couldve" => "could've", "couldnt" => "couldn't", "couldntve" => "couldn't've", "didnt" => "didn't", "doesnt" => "doesn't", "dont" => "don't", "hadnt" => "hadn't", "hadntve" => "hadn't've", "hasnt" => "hasn't", "havent" => "haven't", "hed" => "he'd", "hedve" => "he'd've", "hell" => "he'll", "hellve" => "he'll've", "hes" => "he's", "hows" => "how's", "Id" => "I'd", "Idve" => "I'd've", "Ill" => "I'll", "Illve" => "I'll've", "Im" => "I'm", "Ive" => "I've", "isnt" => "isn't", "itd" => "it'd", "itdve" => "it'd've", "itll" => "it'll", "itllve" => "it'll've", "its" => "it's", "lets" => "let's", "maam" => "ma'am", "mightve" => "might've", "mightnt" => "mightn't", "mightntve" => "mightn't've", "mustve" => "must've", "mustnt" => "mustn't", "mustntve" => "mustn't've", "oclock" => "o'clock", "oughtnt" => "oughtn't", "oughtntve" => "oughtn't've", "shant" => "shan't", "shantve" => "shan't've", "shed" => "she'd", "shedve" => "she'd've", "shell" => "she'll", "shellve" => "she'll've", "shes" => "she's", "shouldve" => "should've", "shouldnt" => "shouldn't", "shouldntve" => "shouldn't've", "sos" => "so's", "thats" => "that's", "thered" => "there'd", "theres" => "there's", "theyd" => "they'd", "theyll" => "they'll", "theyllve" => "they'll've", "theyre" => "they're", "theyve" => "they've", "wasnt" => "wasn't", "wed" => "we'd", "well" => "we'll", "wellve" => "we'll've", "were" => "we're", "weve" => "we've", "werent" => "weren't", "whatll" => "what'll", "whatllve" => "what'll've", "whatre" => "what're", "whats" => "what's", "whatve" => "what've", "whens" => "when's", "whenve" => "when've", "whered" => "where'd", "wheres" => "where's", "whereve" => "where've", "wholl" => "who'll", "whollve" => "who'll've", "whos" => "who's", "whove" => "who've", "whys" => "why's", "willve" => "will've", "wont" => "won't", "wontve" => "won't've", "wouldve" => "would've", "wouldnt" => "wouldn't", "wouldntve" => "wouldn't've", "yall" => "y'all", "yalldve" => "y'all'd've", "yallre" => "y'all're", "yallve" => "y'all've", "youd" => "you'd", "youdve" => "you'd've", "youll" => "you'll", "youllve" => "you'll've", "youre" => "you're", "youve" => "you've", );

$bookTitle = strtr($bookTitle, $translation);
于 2012-10-10T16:04:35.173 回答
0

如果要删除第一个单词中的任何撇号,则只需测试撇号的字符串位置是否低于第一个空格。如果是,则将其删除。

if(strpos($bookTitle,"'") < strpos($bookTitle," ")){
    $bookTitle = preg_replace('/\'/', '', $bookTitle, 1);
}
于 2012-10-10T15:42:19.800 回答
0

在您的链接中使用“It\'s”而不是 IT's

于 2012-10-10T15:28:37.073 回答