Jump to content
R4z3r

[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!

Share this post


Link to post

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 .*

Share this post


Link to post

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.

Share this post


Link to post

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?

Share this post


Link to post

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 );

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...