有条件地从字符串中删除一个单词?

时间:2012-10-10 15:22:41

标签: php

我找到了 类似 的答案,而不是那些完全解决我问题的答案。

用户点击我页面上的链接,将其链接到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时会被删除,我再次获得零结果搜索。

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

OH - 这是奖金 - 仅当字符串中的第一个单词包含撇号时才会发生这种情况。所以它出现在诸如“它不是关于自行车”和“不要眨眼”之类的书籍中。换句话说,撇号似乎并不重要。 (例如作者Bill O'Reilly。)

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

真正难看的解决方案:

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

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

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

然后跑了这个:

$bookTitleTemp = $_GET('booktitle');
$bookTitle = str_replace("W5x32RS97", "'", $bookTitleTemp);
突然间,一切正常。但是GOT是更好的方式。

5 个答案:

答案 0 :(得分:1)

你是不是试图在标题字符串中替换'by \'?我的意思是,而不是: 这不是关于自行车,使用它:它不是关于自行车

答案 1 :(得分: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]);

答案 2 :(得分:0)

在您的链接中 用“它是” 不是IT的

答案 3 :(得分:0)

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

if(strpos($bookTitle,"'") < strpos($bookTitle," ")){
    $bookTitle = preg_replace('/\'/', '', $bookTitle, 1);
}

答案 4 :(得分: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);