ereg problem

Busy's picture

He has: 6,151 posts

Joined: May 2001

Two part question
1/ whats wrong with this:

<?php
if(ereg(\"^[1-9]{6}$\",$title))
{
    
$wher = \" id='\".$title.\"'\";
}else{
    
$wher = \" title like '%\".$title.\"%';
}
?>
$title is a value from a form, if $title is a 6 digit number (min 100000, max 999999) it should use 'id=' and if anything else should use 'title like'.
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%'\";
}
?>
among many other variations.
The '$wher' is part of the database query clause if you hadn't guessed Smiling

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's picture

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's picture

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's picture

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. Wink

Mark Hensler
If there is no answer on Google, then there is no question.

Busy's picture

He has: 6,151 posts

Joined: May 2001

I'm a slow typer lol, I started before Mark had posted

Mark Hensler wrote: 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. Wink

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's picture

He has: 6,151 posts

Joined: May 2001

thanks but isn't quite 100%

I inputted 123abc and it gave me.

Quote: You entered: 123abc

It was a match for: 123 and is 6 numbers only

same as abc123 and wwwww3

cue twilight zone music ...

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

When you find the full solution, let me know. Wink

Busy's picture

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's picture

He has: 4,048 posts

Joined: Aug 2000

Busy wrote: 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.

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] . \"%'\";
    }
}
?>
The string length match is performed in the ereg(). Only a 6 digit number will be accepted. Everything else will fail.

"%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's picture

She has: 5,507 posts

Joined: Feb 2000

Mark, when I tried that I was getting false positive matches for things like abc123456xyz. ?

Busy's picture

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's picture

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's picture

She has: 5,507 posts

Joined: Feb 2000

I amended the demo to match that, Busy, it doesn't work for me?

Busy's picture

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;
' should work fine, is on mine, will only prove true if the number is 6+ numbers only, any letters included it proves false

Suzanne's picture

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's picture

He has: 6,151 posts

Joined: May 2001

kinda weird having a function named after you Wink

the {6,} allows minimum of 6 numbers, change it to {6} for just 6

Quote: Busy
your trying to find (id='12345678')
Suzanne
You entered: 12345678
It should be not 6 numbers only. [12345678] is 8 characters long.
Suzanne No-Regs, No Strlen
You entered: 12345678
It should be 6 numbers only. [12345678] is 8 characters long.

Only because you have this line$suzchar == '6' && ereg("([0-9]{6,})' in suzanne function; suzchar == 6 but ereg >= 6
everything else seems to work ok on all three

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Ah, yes, thank you.

So, fixed. Smiling And I feel illuminated! Sorry for hijacking your thread in my quest to understand the world. heh.

Busy's picture

He has: 6,151 posts

Joined: May 2001

no worries Smiling

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.