"Hello World".gsub("Hello", "Bye")
Tag Archive for search
String replace in ruby
Customize search results
<?php
/**
* Theme override for search results.
*
* Replace themename with your theme's name.
*/
function themename_search_item($item, $type) {
$output = ' <dt class="title"><a href="'. check_url($item['link']) .'">'. check_plain($item['title']) .'</a></dt>';
$output .= ' <dd>'. ($item['snippet'] ? '<p>'. $item['snippet'] .'</p>' : '');
return $output;
}
?>
Non collapsible advanced search
<?php
/**
* Theme override for search form.
*
* Replace themename with your theme's name.
*/
function themename_search_form($form) {
$output = '';
// Print out the $form array to see all the elements we are working with.
//$output .= dsm($form);
// Once I know which part of the array I'm after we can change it.
// You can use the normal Form API structure to change things, like so:
// Change the advanced search field to non collapsible.
$form['advanced']['#collapsible'] = 0;
// Make sure you call a drupal_render() on the entire $form to make sure you
// still output all of the elements (particularly hidden ones needed
// for the form to function properly.)
$output .= drupal_render($form);
return $output;
}
?>
Create a Firefox Search Plugin
<search name="Digital Inspiration" method="GET" action="http://www.google.com/search" queryCharset="utf-8" > <input name="q" user> <input name="sitesearch" value="labnol.blogspot.com"> </search>
Approximate / Fuzzy search base.
<?php
class FuzzySearch {
public $string;
public $search;
public function __construct($string = null) {
if(!empty($string)) {
self::SetString($string);
}
}
public function SetString($string) {
if(is_string($string)) {
$this->string = $string;
}
}
public function SetSearch($search) {
if(is_string($search)) {
$this->search = $search;
}
}
public function PreformSearch($search, $string = null, $threshold = 1, $caseSensitive = false) {
if(empty($search) && !empty($this->search)) { $search = $this->search; }
if(empty($string) && !empty($this->string)) { $string = $this->string; }
$matches = array();
if(isset($string) && strlen($string) > 0) {
$words = explode(" ", $string);
foreach($words as $word) {
$stripedWord = self::StripFormatting($word);
if(self::CompareStrings($search, $stripedWord, $caseSensitive) <= $threshold) {
$matches[] = $stripedWord;
}
}
}
return $matches;
}
public function CompareStrings($str1, $str2, $caseInsensitive = false) {
$threshold = 0;
if($caseInsensitive) {
$str1 = strtolower($str1);
$str2 = strtolower($str2);
}
if(strlen($str1) != strlen($str2)) {
if(strlen($str1) > strlen($str2)) {
$threshold = strlen($str1) - strlen($str2);
}
else if(strlen($str2) > strlen($str1)) {
$threshold = strlen($str2) - strlen($str1);
}
}
for($i = 0; $i < strlen($str1); $i++) {
if($i <= strlen($str2) - 1) {
if($str1{$i} != $str2{$i}) {
$threshold++;
}
}
}
return $threshold;
}
public function StripFormatting($str) {
$formatting = array( ".", ",", ";", ":", "|", "[", "]", "{", "}", "(", ")", "=", "+",
"!", "@", "#", "$", "%", "^", "&", "*", "/", "", "<", ">", "?");
$str = str_replace($formatting, "", $str);
$str = trim($str);
return $str;
}
}
?>
in_array compact version
var in_array = function(s,a) {for(var k in a) if(a[k]==s) return 1; return 0;}
grep for either of two strings
lwp-request -o links <a href="http://icanhascheezburger.com/" >http://icanhascheezburger.com/</a> | grep -iE "(jpg|gif)"
Shell script to make lists of certain file types in a web site file system
mkdir file_lists find . -name '*.pdf' -o -name '*.PDF' > ./file_lists/all_pdf_files.txt find . -name '*.pdf' -o -name '*.PDF' -mtime +365 > ./file_lists/pdf_files_mtime_gt_1yr.txt find . -name '*.jpg' -o -name '*.JPG' -o -name '*.gif' -o -name '*.GIF' -o -name '*.png' -o -name '*.PNG' > ./file_lists/all_image_files.txt find . -name '*.log' -o -name '*.LOG' > ./file_lists/all_log_files.txt find . -name '*.css' -o -name '*.CSS' -o -name '*.js' -o -name '*.JS' > ./file_lists/all_design_assets_files.txt find . -name '*.ram' -o -name '*.RAM' -o -name '*.aif' -o -name '*.AIF' -o -name '*.mp3' -o -name '*.MP3' > ./file_lists/all_audio_files.txt find . -name '*.ram' -o -name '*.RAM' -o -name '*.aif' -o -name '*.AIF' -o -name '*.mp3' -o -name '*.MP3' -mtime +365 > ./file_lists/audio_files_mtime_gt_1yr.txt find . -name '*.html' -o -name '*.HTML' -o -name '*.htm' -o -name '*.HTM' > ./file_lists/all_html_files.txt find . -name '*.html' -o -name '*.HTML' -o -name '*.htm' -o -name '*.HTM' -mtime +365 > ./file_lists/html_files_mtime_gt_1yr.txt
Launch Google Search in default Browser
/// <summary>
/// Class to launch Google Search in default browser.
/// </summary>
public static class Google {
/// <summary>
/// Launches Google Search in default browser, and escapes string according to: <a href="http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping" >http://code.google.com/apis/searchappliance/documentation/46/xml_reference.html#appendix_url_escaping</a>
/// </summary>
/// <param name="searchQuery">The search query.</param>
public static void SearchGoogle(string searchQuery) {
string fixedSearchQuery = null;
foreach (char character in searchQuery) {
if (Char.IsLetterOrDigit(character)) {
fixedSearchQuery += character;
} else if (character == Char.Parse(" ")) {
fixedSearchQuery += "+";
} else {
fixedSearchQuery += Uri.HexEscape(character);
}
}
string url = @"http://www.google.com/search?hl=en&q=" + fixedSearchQuery;
try {
Process.Start(url);
} catch { }
}
}
Ruby Ferret simple indexing and search example part 1
require 'rubygems'
require 'ferret'
require 'find'
include Ferret
index = Index::Index.new(:default_field => 'content', :path => '/tmp/index_folder')
ini = Time.now
numFiles=0
IndexedExts=['.java','.properties']
Find.find('/code/to/index') do |path|
if(IndexedExts.find {|ext| path.include?(ext)}==nil)
next
end
puts "Indexing: #{path}"
numFiles=numFiles+1
if FileTest.file? path
File.open(path) do |file|
index.add_document(:file => path, :content => file.readlines)
end
end
end
elapsed = Time.now - ini
puts "Files: #{numFiles}"
puts "Elapsed time: #{elapsed} secs
"