Jump to content

[PHP] Leverage Alfred Search to find closest match in array


Recommended Posts

Hey,

 

I was wondering if there was a good way to leverage the search autocomplete via PHP. I am looking to add easy timezone support to my workflow (based on these timezones — http://www.php.net/manual/en/timezones.php). I would like to make a PHP array with autocomplete suggestions for the user to make it easier for them to set the timezone. I believe I could use PHP's levenshtein function to find the closest matches to the input, but was wondering if there is a better way of doing this before passing it to the script to save to the JSON config file.

 

I would like, if possible, to first autocomplete the country, then once the backslash appears to begin matching against the locations in that country. If there is a best way to utilize alfred to do this, please let me know. If not, I can use a combo of levenshtein and conditionals I think to do it. It may not be hard to modify one line of code, but anyway to make it easier for people is better.

 

Thanks!

Link to comment

Hey,

 

I was wondering if there was a good way to leverage the search autocomplete via PHP. I am looking to add easy timezone support to my workflow (based on these timezones — http://www.php.net/manual/en/timezones.php). I would like to make a PHP array with autocomplete suggestions for the user to make it easier for them to set the timezone. I believe I could use PHP's levenshtein function to find the closest matches to the input, but was wondering if there is a better way of doing this before passing it to the script to save to the JSON config file.

 

I would like, if possible, to first autocomplete the country, then once the backslash appears to begin matching against the locations in that country. If there is a best way to utilize alfred to do this, please let me know. If not, I can use a combo of levenshtein and conditionals I think to do it. It may not be hard to modify one line of code, but anyway to make it easier for people is better.

 

Thanks!

 

The levenshtein function is quite useful. I have also used this code:

$fuzzy = preg_replace('/(.)/', '$1.*', $query);
preg_match("/$fuzzy/i", $haystack);

 

It's been a while since I used this (and it may not work as I expect), but it's supposed to replace each character with the character and a fuzzy space .*

Link to comment

Are you wanting the user to be able to set THEIR timezone? Reason I ask is, you can actually read that from the system without having to ask the user to set it.

 

Reading the default timezone through PHP though equates to errors since PHP timezones aren't set. How would I ask Alfred for this information?

 

Also, would this be able to read in the system clock preferences for 12-hour vs. 24-hour time?

Link to comment

In PHP:

$tz = exec( 'systemsetup -gettimezone | cut -d " " -f 3 ' );

 

That reads the timezone that the machine is set to. In my case, this returns "America/Chicago". So you could set the default timezone by doing....

$tz = exec( 'systemsetup -gettimezone | cut -d " " -f 3 ' );
date_default_timezone_set( $tz );
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...