ereg problem
Two part question
1/ whats wrong with this:
<?php
if(ereg(\"^[1-9]{6}$\",$title))
{
$wher = \" id='\".$title.\"'\";
}else{
$wher = \" title like '%\".$title.\"%';
}
?>
No matter what I input it's always going to the else statement.
also tried
<?php
if(eregi('[0-9]{6}',$title))
{
$wher = \" id=$title\";
}else{
$wher = \" title like '%$title%'\";
}
?>
The '$wher' is part of the database query clause if you hadn't guessed
2/ the above is a cut down version of the script, another form option is exact phrase or contains, which is either title like '$title' or title like %$title%.
title like %$title% works
title like '$title' doesnt work
what am I doing wrong this time?
Suzanne posted this at 04:05 — 15th February 2004.
She has: 5,507 posts
Joined: Feb 2000
http://www.synapticimpulse.com/testy.php
<?php
if (isset($_POST['title'])) {
if (strlen($_POST['title']) == '6' && ereg (\"([0-9]{1,6})\", $_POST['title'], $regs)) {
echo 'id= '. $regs[0];
} else {
echo \"title like '%\" . $_POST[title] . \"%'\";
}
}
?>
Check the link above for a quick form that tests whether this is working or not.
Suzanne posted this at 04:55 — 15th February 2004.
She has: 5,507 posts
Joined: Feb 2000
Oh, for the second problem, it's likely something to do with the single quotes being non-parsed. See my sample?
Mark Hensler posted this at 07:42 — 15th February 2004.
He has: 4,048 posts
Joined: Aug 2000
The SQL search term "LIKE" accepts wildcards (which are percent signs).
WHERE field LIKE 'string'
will only match "string", not "string cheese"
You know I just had to put that example in.
Mark Hensler
If there is no answer on Google, then there is no question.
Busy posted this at 08:17 — 15th February 2004.
He has: 6,151 posts
Joined: May 2001
I'm a slow typer lol, I started before Mark had posted
Ok, so if
WHERE field LIKE %cat%
will find catch, catnmouse, cathouse ...
how do I find cat in 'cat house' or 'cat eating the mouse who ate the cheese' and even just 'cat'
WHERE field = cat
only wants to find cat but not 'cat house' etc
Busy posted this at 08:07 — 15th February 2004.
He has: 6,151 posts
Joined: May 2001
thanks but isn't quite 100%
I inputted 123abc and it gave me.
It was a match for: 123 and is 6 numbers only
same as abc123 and wwwww3
cue twilight zone music ...
Suzanne posted this at 20:40 — 15th February 2004.
She has: 5,507 posts
Joined: Feb 2000
When you find the full solution, let me know.
Busy posted this at 23:58 — 15th February 2004.
He has: 6,151 posts
Joined: May 2001
sorted it out, (ereg("^[0-9]{6}$",$title)) works but (ereg("^[1-9]{6}$",$title)) doesn't only difference is one starts with 0, the other 1.
also discovered I can use {6,} which should mean six or more (it works anyway)
Thanks all,
now just have to figure out the like and %like% thing
Mark Hensler posted this at 02:54 — 16th February 2004.
He has: 4,048 posts
Joined: Aug 2000
Woah! Be careful there. That wont match any numbers with a zero in it. So it wont match "100000" or "123450", etc.
Try:
<?php
if (isset($_POST['title'])) {
if (ereg(\"^([0-9]{6})$\", $_POST['title'], $regs)) {
echo 'id= '. $regs[0];
} else {
echo \"title like '%\" . $_POST[title] . \"%'\";
}
}
?>
"%cat%" will match "cat" as well as any string with "cat" in it (begining/ending with or containing).
Mark Hensler
If there is no answer on Google, then there is no question.
Suzanne posted this at 05:03 — 16th February 2004.
She has: 5,507 posts
Joined: Feb 2000
Mark, when I tried that I was getting false positive matches for things like abc123456xyz. ?
Busy posted this at 05:07 — 16th February 2004.
He has: 6,151 posts
Joined: May 2001
yeah sorted the 1-9 and 0-9 thing, but with the search, I want two options
one if looking for 'contains' will find cat, catch, catty, cathy ... but also want one that will find 'exact' matches only, search for 'cat' will find "cat" or "cat and dog", "smelly cat" etc.
problem i have is looking in db rows it will only find "cat", wont find anything else, like "cat and dog"
Busy posted this at 05:11 — 16th February 2004.
He has: 6,151 posts
Joined: May 2001
if(ereg("^[0-9]{6,}$",$titled))
{
is numbers
}else{
isn't 6+ numbers
}
works, 6+ numbers only = true, numbers and letters = false
Suzanne posted this at 15:53 — 16th February 2004.
She has: 5,507 posts
Joined: Feb 2000
I amended the demo to match that, Busy, it doesn't work for me?
Busy posted this at 21:02 — 16th February 2004.
He has: 6,151 posts
Joined: May 2001
only difference to my version is I'm not using the $regs, nor the $_GET ($_POST in your sample) as the content is trimed and striped etc before being checked for length and numbers.
try just the number check without isset or globals
if(ereg("^[0-9]{6,}$",$title))
{
$answer = " (id='".$title."')";
}else{
$answer = " (title like '%".$title."%')";
}
echo "your trying to find " .$answer;
Suzanne posted this at 00:04 — 17th February 2004.
She has: 5,507 posts
Joined: Feb 2000
I updated this. I'm getting varied results. Perhaps it's just me.
No, the problem seems to be that without strlen, the all numbers version is getting through even if more than 6 characters.
Busy posted this at 07:50 — 17th February 2004.
He has: 6,151 posts
Joined: May 2001
kinda weird having a function named after you
the {6,} allows minimum of 6 numbers, change it to {6} for just 6
Only because you have this line
$suzchar == '6' && ereg("([0-9]{6,})
' in suzanne function; suzchar == 6 but ereg >= 6everything else seems to work ok on all three
Suzanne posted this at 16:46 — 17th February 2004.
She has: 5,507 posts
Joined: Feb 2000
Ah, yes, thank you.
So, fixed. And I feel illuminated! Sorry for hijacking your thread in my quest to understand the world. heh.
Busy posted this at 06:10 — 18th February 2004.
He has: 6,151 posts
Joined: May 2001
no worries
it's good to see the many ways of doing things, and learning from errors.
Everyday is a learning day **now if only my small brain would contain it all**
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.