用户输入数据库

Suppose that, we're expecting just strings or numbers with the data send by a user. Is it safe enough to check the data with ereg and preg_match functions? Is there a way to fake them? Should we still use mysql_real_escape_string?

safe enough is relative to your own needs. If you're wanting to avoid mysql_real_escape_string for some reason then I first want to ask why.

My answer is: sure... depending on your conditions

you can preg match against [0-9a-z] and there is nothing to fear. Try passing a multibyte character to be safe. So long as your condition does not allow you to do anything if the match does not fit your requirements then there is no tricky work-around that I know of to slip in malicious characters on such a strict rule.

but the term "string" is very open. does that include punctuation? what kind, etc. If you allow standard injection characters as what you call a "String" then my answer is no longer sure.

But I still recommend mysql_real_escape_string() on all user submitted info, no matter how you try to purify it before hand.

This will be short answer...

Use PDO:

For example Zend famework is using this engine.

If you use a regex to match against valid input, and it succeeds, then the user input is valid. That being said, if you don't have any malicious characters in valid input (particularly quotes or potentially multibyte characters), then you don't need to call mysql_real_escape_string. The same principle applies to something like:

$user_in_num = intval( $_POST['in_num']); // Don't need mysql_real_escape_string here

So something like the following:

$subject = $_POST['string_input'];
if( !preg_match('/[^a-z0-9]/i', $subject)) 
{
    exit( 'Invalid input');
}

It is fine / safe to use $subject in an SQL query once the preg_match succeeds.