I have SQL rows with this information: Example - Exampleish (Exampleishbosh)
not all have () in them, so would it be a good idea to strip the
- and ()
out of the url and then just do a search with the remaining words in the SQL query?
http://www.co.com/example_exampleish_exampleishbosh/
or is this a bad idea?
Since the question is a bit unclear to me, I'm assuming that the information string acts as a key-sort-of-thing for additional information (like a title for an article), and there is no true GET search requirement.
If this is the case, if allowed to modify the database structure, I would use some kind of surrogate key instead (like a numeric identifier), using the title-thingy as a slug.
In this case, your when generating links in your website, you get both the id and slug for the target, and create an url of it in the format http://example.com/[id]/[slug]/
, for example http://example.com/213/omg-lol-the-cat-jumped/
.
When processing the request in your server, you are only interested in the id ("213"), and the slug ("omg-lol-the-cat-jumped") is just for the human user to give a sneak-peek of the title, and doesn't affect the search result. You provide the page that matches the id, not the slug. This way, your searching is efficient (no SQL LIKE-matching), and less error prone (SQL injections from malformatted slugs).
As for the (possible?) question about how to convert a string into a slug, there are many approaches to that. I would simply remove all non-alphanumeric characters, and combine the alphanumeric chunks with dashes or underscores. You probably want to cut the string length to 20-ish characters.
If I understand correctly you're looking for a slug-like functionality, if so here is the function that you should use to "slugify" ASCII strings:
function Slugify($string)
{
return strtolower(trim(preg_replace('~-+~', '-', preg_replace('~[^0-9a-z]~i', '-', $string)), '-'));
}
Slugify('Example - Exampleish (Exampleishbosh)'); // example-exampleish-exampleishbosh
You should store the slug in the database and match it with the resquested URL.
EDIT: This function can be simplified a little more:
function Slugify($string)
{
return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', $string), '-'));
}