Created
April 1, 2017 19:27
-
-
Save tpkemme/4e6b8b7177c72cf2e02758f04e0f88c9 to your computer and use it in GitHub Desktop.
Generates lorem ipsum from lipsum.com for Alfred Workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This php script queries a form at http://www.lipsum.com/ to get a | |
* hashed version of a provided or default password | |
* | |
* This script is run using the command 'lipsum p 4' where p stands for | |
* paragraph and 4 is the number of paragraphs. Other option is w for words. | |
* | |
*/ | |
// Query in the form of two parameters 'type' and number of 'type' | |
$query = "{query}"; | |
$type = ''; | |
// There are no arguments... | |
if( empty( $query ) ){ | |
// No type, use default 'paragraph' | |
$type = 'paras'; | |
$num = '1'; | |
} | |
// At least 1 argument exists | |
else{ | |
// Turn query string into params array | |
$params = explode(' ', $query); | |
// Get provided type | |
$type = $params[0]; | |
if( $type === 'p' ){ | |
$type = 'paras'; | |
} | |
else if( $type === 'w' ){ | |
$type = 'words'; | |
} | |
else{ | |
$type = 'paras'; | |
} | |
// Get Number of type if available | |
if( $params[1] ){ | |
$num = $params[1]; | |
} | |
else{ | |
$num = 1; | |
} | |
} | |
$hashUrl = 'http://www.lipsum.com/feed/html?what='. $type . '&amount=' . $num; | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'GET') | |
); | |
$context = stream_context_create($options); | |
$output = file_get_contents( $hashUrl, false, $context ); | |
// Extract lipsum | |
$first_step = explode( '<div id="lipsum">' , $output ); | |
$second_step = explode('</div>' , $first_step[1] ); | |
$return = str_replace( '</p>', '', str_replace('<p>', '', $second_step[0])); | |
$tmp = explode("\n", $return); | |
array_shift($tmp); | |
array_shift($tmp); | |
$final = implode( "\n", $tmp ); | |
echo $final; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment