加扰PHP字符串

I want to create Unique ID for my site users in this format

pubID-john_34032

So I want to do this using the email string.

I have this pseudo code (but I want it faster and more logical if that makes sense)

How do I write my Pseudo code as PHP?

<?php
$var e(mysqlescape(get(emailform));
$var r(rand(3,34);
$var ex=(pubID-);
$var e=(replace(@/.); //users may enter 1@site.com so replace the at and .
$var e(first4strTo(e)); // get first four letters, example 1sit 
$var e(e)+r(); // e is now 1site-1547
$var printStr e+ex //join the 1site-1547 with pubID- and done
?>

Use str_replace() to remove the @ and . characters then use uniqid() to generate a unique identifier. Finally, concatenate them all together.

$email = 'user@gmail.com';
$str = 'pubID-' . str_replace(array('@', '.'), '', $email) . '_' . uniqid();
echo $str;

Unique Id in PHP there is uniqid. You can append that to whatever string. Why reinvent the wheel?

I fail to see your reasoning here. Why: pubID-john_34032

Couldn't you use the the user's name and append an auto incrementing id generated by your database? Something like:

calvinfroedge_23923

Then all you have to do is replace non whitelisted characters in the names and put an underscore between the name and the id.

I don't have the reputation points to respond on Emmanuel N's answer.

Here's how to use uniqid and just grab the first part of the email address.

uniqid(substr($email, 0, strpos($email, '@')) . '_')