I have tried to grab api ids for yelp, tripadviso, foursquare & twitter based on their url. I did it with regular expressions and the "preg_match" function. however I encountered a very strange phenomenon.
$yelp_option = 'https://www.yelp.com/biz/belved%C3%A8re-delft-2';
$foursquare_option = 'https://www.tripadvisor.nl/Restaurant_Review-g188626-d2375308-Reviews-Belgian_Beer_Cafe_Belvedere-Delft_South_Holland_Province.html';
$tripadvisor_option = 'https://foursquare.com/v/belv%C3%A9d%C3%A8re/4ad8dc0ff964a520601521e3';
$twitter_option = 'https://twitter.com/duitdaging';
if(strpos($yelp_option, 'biz/')){
preg_match("/(?<=biz\/).*/", $yelp_option, $output_array);
$yelp_option = $output_array[0];
}
if(strpos($foursquare_option, '/')){
preg_match("/[^/]+$/", $foursquare_option, $output_array);
$foursquare_option = $output_array[0];
}
if(strpos($tripadvisor_option, '-d')){
preg_match("/(?<=-d)(.*?)(?=-)/", $tripadvisor_option, $output_array);
$tripadvisor_option = $output_array[0];
}
if(strpos($twitter_option, '/')){
preg_match("/[^/]+$/", $twitter_option, $output_array);
$twitter_option = $output_array[0];
}
The output was very unexpected....
$yelp_option = 'belved%C3%A8re-delft-2';
$foursquare_option = '2375308';
$tripadvisor_option = '4ad8dc0ff964a520601521e3';
$twitter_option = '2375308';
I tired for 1½ hour to move stuff around, comment out stuff... nothing seemed logical. Why does $twitter_option become the same as $foursquare_option???
is it because the regex pattern is the same? I tried adding a . to the twitter pattern so it look like so: [^/]+.$
- now it's different but the regex should produce the same right? Still it became the same as $foursquare_option...
I tired flipping the order around so twitter preg_match executes before the foursquare one, but the result was not as I expected, I would think both variables would now be duitdaging but instead both was an empty string....
When I comment out the entire if(...){foursquare regex...} the twitter one works fine and produces duitdaging. But when I have both of them it just wont work.
I solved it easyli by changing the twitter sequence to so:
if(strpos($yelp_option, 'biz/')){
preg_match("/(?<=biz\/).*/", $yelp_option, $output_array);
$yelp_option = $output_array[0];
}
if(strpos($foursquare_option, '/')){
preg_match("/[^/]+$/", $foursquare_option, $output_array);
$foursquare_option = $output_array[0];
}
if(strpos($tripadvisor_option, '-d')){
preg_match("/(?<=-d)(.*?)(?=-)/", $tripadvisor_option, $output_array);
$tripadvisor_option = $output_array[0];
}
if(strpos($twitter_option, "/")){
$pieces = explode("/", $twitter_option);
$twitter_option = end($pieces);
}
so I don't use preg_match for both twitter and foursquare. This gave the right outcome
$yelp_option = 'belved%C3%A8re-delft-2';
$foursquare_option = '2375308';
$tripadvisor_option = '4ad8dc0ff964a520601521e3';
$twitter_option = 'duitdaging';
I'm still more confused than I have ever been so I just MUST ask this question. Does this seem logical to anyone?
- Happy friday x_x