Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringHelper: new proposed methods #364

Closed
tonydspaniard opened this issue May 23, 2013 · 6 comments · Fixed by #464
Closed

StringHelper: new proposed methods #364

tonydspaniard opened this issue May 23, 2013 · 6 comments · Fixed by #464

Comments

@tonydspaniard
Copy link
Contributor

@qiangxue
Copy link
Member

toAscii should be moved to Inflector. The slug method may reuse it.

@samdark
Copy link
Member

samdark commented May 23, 2013

What's the use for uuids? Never used it in PHP apps.

@tonydspaniard
Copy link
Contributor Author

@samdark The main function of an UUID is being unique.

@samdark
Copy link
Member

samdark commented May 23, 2013

Why not using microtime or uniqid, for example? Is it really that useful in typical projects?

@tonydspaniard
Copy link
Contributor Author

In my case, I normally use a custom class:

class HUniqueId
{

    /**
     * Using uniqid() function
     * @param string $prefix
     * @param bool $entropy
     * @param bool $hash
     * @return string
     */
    public static function generateBasic($prefix = '', $entropy = true, $hash = false)
    {
        $id = uniqid($prefix, $entropy);
        return $hash ? md5($id) : $id;
    }

    /**
     * Generate unique using current time + IP
     * @return mixed
     */
    public static function generateByTimeAndIp()
    {
        $timeStamp = date('Ymdhis');
        $ip = $_SERVER['REMOTE_ADDR'];
        $id = "{$timeStamp}-{$ip}";
        return str_replace('.', '', $id);
    }

    /**
     * Generate custom length unique id
     * @param int $length
     * @return string
     */
    public static function generateByLength($length = 10)
    {
        $id = crypt(uniqid(rand(), 1));
        $id = strip_tags(stripslashes($id));
        $id = str_replace('.', '', $id);
        $id = strrev(str_replace('/', '', $id));
        return substr($id, 0, $length);
    }

    /**
     * Generate XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX style unique id,
     * (8 letters)-(4 letters)-(4 letters)-(4 letters)-(12 letters)
     */
    public static function generateGuid()
    {
        $s = strtoupper(md5(uniqid(rand(), true)));
        return
            substr($s, 0, 8) . '-' .
                substr($s, 8, 4) . '-' .
                substr($s, 12, 4) . '-' .
                substr($s, 16, 4) . '-' .
                substr($s, 20);
    }
}

But some people like uuid for its strengh

@tonydspaniard
Copy link
Contributor Author

ok, we'll leave uuid and move toAscii on Inflector helper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants