Tag: search

Searching a string – do you need to start with a period?

Searching a string – do you need to start with a period?

Today I had to write some code to search for text that matched various file extensions, this should have been a no brainer of a task. But like some things that sound stupidly simple it caught me out.

If you have a string and you use the search method then when it finds something it will return the position. If it doesn’t then it will return -1. So far so good.

Now I could go into huge detail (I did, but it ended up to wordy/dull, so I just deleted it!).
So if you EVER have to search for a string and your string starts with a period ‘.’ then add a double backslash ‘\’.
If you don’t then the period will match anything! e.g. you want to match ‘.ra’ and the string contains the word brain then ‘brain’.search( ‘.ra’ ) DOES NOT return -1!

use ‘\.ra’ for correct results.

Not sure why ‘.ra’ doesn’t work, maybe it’s treating the string like a regEx, I know search can take both a string and a regEx so maybe its not looking at its type and guessing that it should be a regEx?? You can’t step into the code either to see why, so just use the \

Very annoying!

Previous Tip

[ad name=”ad-1″]

Flex Collection – Google search

Flex Collection – Google search

Totally off topic this. But I had quite a bit of hassle trying to find info on how to do what I just did.

So if you look to your right you’ll see a nice search box with the Google brand inside it 🙂 Nothing special there you may think… well.

Well it’s a custom search box from Google, and I’ve customised it by adding all of my favourite and preferred flex, AIR, and flash sites. So should you need to search on anything related to the mentioned categories just use the search box to the right.

 

How its done

If you are interested in adding something similar to your blog or web page then there is very little information out there about Google’s custom search page. 

There seems to be 2 ways to add it to wordpress.

1)       Add it as a page within your site so that it retains your sites look and feel, in order to do this you can find a plugin for custom google search and depending on what theme you have have this may or may not work. I tried out http://aleembawany.com/projects/wordpress/google-custom-search-plugin/ which as it happens didn’t work with my theme.  But I wasn’t really trying to get it to work inside my site, I wanted the custom search to retain the Google style as that is what people are used to when using Google.

2)      Add it as a hosted Google custom search. Now on this method I found absolutely zero information (actually the info I did find on the wordpress site said it wasn’t possible).  Not to be put off by this info as anything seems to be possible with wordpress (I’m just learning it really, my blog is only a few weeks old), I decided to hunt through the various php files etc that come with wordpress.

The file that is required is searchform.php and this can be found inside the theme folder that you are using ( something like ‘wp-content/themes/{your theme folder}/searchform.php’)

Open up the file and inside that you should find some php and some html code (a form).  Replace the html code with the code that Google has given you for your custom search.  Or if you would like to have a search that just does Flex, AIR and flash sites then feel free just to use the following code.

 



When the user searches in this form then it will look up just a list of sites that only contain information on Flex, AIR, actionscript & flash.

You can also bookmark the following link

http://www.google.com/cse?cx=partner-pub-7396620608505330:xjbbr6-w0cu&ie=ISO-8859-1

This will link up to the specific Google search that I’ve set up just to look at relevant Flex, AS3, AIR & Flash sites.

 

[ad name=”ad-1″]

Â