0

I have been trying to use the YouTube API to search for videos from YouTube using the Zend Gdata class. When I type a search term with a single word I get back expected results but when I start using more than one work in my search term I dont seem to get back very good results, the videos do not seem to be related to my search term.

I urlencode the search term before passing it to youtube - ie

$query->videoQuery = urlencode($searchString);

Has anyone come across a similar problem? any solutions?

undefined
  • 5,190
  • 11
  • 56
  • 90

1 Answers1

1

I'm not exactly sure how the Youtube API provides search results but you can try the following:

Option 1: (Adds quotes around the query)

$query->videoQuery = urlencode('"' . $searchString . '"');

Option 2: (Adds a '+' in front of each word)

$query->videoQuery = urlencode('+' . preg_replace('/\s+/', ' +', $searchString));
Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
Chris L.
  • 275
  • 3
  • 10
  • Would it be faster to just do `str_replace(' ',' +', $searchString);`? Also, your query requires that at least two spaces are required to replace them with `+`. Use `'/\s+/'` instead :P. – Konrad Borowski Aug 26 '11 at 16:00
  • 1
    Good catch! Yeah the preg_replace should be /\s+/ (answer edited). The problem with doing a str_replace(' '...) is if someone has a search string with two spaces 'foo bar' it would become 'foo + +bar' – Chris L. Aug 26 '11 at 16:12
  • Its that simple! thanks Chris, option 1 worked great. FYI when I tried option 2 it didnt return any results but option 1 returned expected videos for my search string. thanks! – undefined Aug 29 '11 at 11:31