removing hyphen from search string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

removing hyphen from search string

Post by mikeeeeeeey »

Hi guys,

I've been having some major problems with a script I'm developing which basically queries the database using a SELECT statement. The trouble is some of the entries contain hyphen's and this is letting strings past which should have been found in the database.

I've tried replacing the character beforehand using str_replace() and used addslashes() and mysql_real_escape_string() but to no avail. I've just read up a bit on Regex and I was just wondering is there any way I can use this to achieve the result I'm after.


Here's the SQL below (you'll appreciate why I'm tearing my hair out):

Code: Select all

$sql   = "SELECT * FROM categories_description WHERE categories_name = '" . $section . "'";
Can anyone help at all?

Thanks for reading.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

You may find REPLACE(str,from_str,to_str) useful.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

Thanks, still the same output though.
Am I right in thinking there is a MySQL bug concerning hyphens and its effect on a SELECT statement?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

As far as I can understand you want this to be found:

$section = "bla bla";

categories_name entiries:
bla-bla
bla bla
bla-\nbla

is it right?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

Say...
$section[0] = "blah - blah";
$section[1] = "blah - blah";

Everytime I examine a $section I want to query this to categories_name and, if there are no other entries, insert that into categories_name, but so far the example above would insert both $section strings (since they contain a hyphen).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

I've tested your example and it worked ...
Try echoing the $sql variable and see what it contains.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post by mikeeeeeeey »

SELECT `categories_name`
FROM `categories_description`
WHERE `categories_name` = CONVERT( _utf8 'Visual - Conference & Presentation'

it should say 'Audio Visual - Confere...'

This is something I've put together from PHPmyadmin. Why is it removing the first word?
Post Reply