• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handy string functions

#1
[eluser]bena[/eluser]
Hi peeps,

Thought I'd share some functions I find quite handy when I'm developing. There's also a little guide for anyone interested in using them.

is_str() - a more solid version of the is_string() function
str_append() - appends a string to a string if not already present
str_prepend() - prepends a string to a string if not already present
str_convert_special() - converts special characters to an appropriate equivalent (I think this was based on something I saw in wordpress)
str_numonly() - strips everything except numbers from a string (lazy I know)
str_safemail() - formats an email address for safer use in HTML pages
_now() - technically belongs in a date helper... it's just like CI's now() function but supports a few different output formats (date|time|datetime|unix)

Please let me know if there are already functions out there doing the same thing! Smile Hope someone finds them useful.

Regards,
Ben

#2
[eluser]Yash[/eluser]
some of these I'm already using. anyways thanks for sharing

#3
[eluser]bena[/eluser]
I forgot about this one too:

Code:
function strip_dbl_space($input=NULL)
{
    if (empty($input) OR !is_str($input))
    {
        return $input;
    }

    $input = trim($input);
    $words = explode(' ', $input);

    foreach ($words as $key => $word)
    {
        $word = trim($word);

        if (empty($word))
        {
            unset($words[$key]);
        }
    }

    return implode(' ', $words);
}

#4
[eluser]Shay Falador[/eluser]
Thanks for sharing although I can't find this functions useful actually.
By the way, the function str_numonly, is the same as casting to int ($string = (int)$stringWink except it would probably be slower =]

#5
[eluser]bena[/eluser]
Excellent I didn't realise, I'll bear it in mind thanks man.

#6
[eluser]bena[/eluser]
Hi,

I was having a play with the (int) method u mentioned and it didn't quite do what I thought you meant:

Code:
$string = 'testing 12 some numb3rs';
echo (int) $string . '<br />' . str_numonly($string);

# Output:
# 0
# 123

I also found a few problems with the is_str() function (arrays and objects were accepted as strings...) so it now looks like this:

Code:
function is_str($input)
{
    $functions = array( 'is_float', 'is_int', 'is_numeric', 'is_string' );
    $not_string = FALSE;

    foreach ($functions as $key => $function)
    {
        $functions[$key] = "!{$function}".'($input)';
    }

    $condition = implode($functions, ' && ');
    $cmd = 'if ( '.$condition.' ) { $not_string = TRUE; }';
    eval($cmd);

    if ($not_string)
    {
        return FALSE;
    }

    return TRUE;
}

#7
[eluser]Shay Falador[/eluser]
You are right, it doesn't work as I taught it was, it keeps the first digits, until it sees an non-digit charter.

Code:
echo (int)'12test3';
Outputs 12.

Code:
echo (int)'test123';
Outputs 0.

My bad.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.