PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 07, 2020 10:53 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Useful String Functions
PostPosted: Fri Dec 03, 2004 9:03 am 
Offline
Forum Contributor
User avatar

Joined: Fri Jun 21, 2002 7:00 pm
Posts: 353
Location: Cleveland, OH
I have written two string unctions which I have found to be quite useful. The first function, str_insert() allows the user to insert a string into an old string at a defined string index. The second function, str_multi_insert() does the same thing, but lets you insert multiple strings at the same time.

Syntax: [ Download ] [ Hide ]
<?php

/**

 * Inserts a string before the index set my $strpos of the $old_string that we pass in.

 *

 * @author  protokol

 * @version 1.0

 * @date    Thu Dec 02 15:00:04 EST 2004

 * @param   string $insert_string The string to insert into the $old_string

 * @param   int $strpos The index to insert the string before in the $old_string

 * @param   string $old_string The string that we want to manipulate

 * @return  string The newly created string

 */


function str_insert ($insert_string, $strpos, $old_string)

{

    $strlen = strlen($old_string);

   

    // If the string position is off the end of the string index, set it to the length of the string

    // which would be the last index in the string plus 1. If the position is less than zero, then

    // treat that number as the index starting from the end of the string. For example, if $strpos is -1,

    // then the strpos we want to reference is the last index of the string. This will place the $insert_string

    // before this index, basically placing it right before the last character in the string.

    if ($strpos > $strlen) {

        $strpos = $strlen;

    } else if ($strpos < 0) {

        $strpos += $strlen;

    }

   

    // Create the first part of the string up to the point we want to insert the new string at

    $new_string  = substr($old_string, 0, $strpos);

   

    // Tack on the string we are inserting

    $new_string .= $insert_string;

   

    // Finish it off by getting the rest of the original string starting at $strpos

    $new_string .= substr($old_string, $strpos);

   

    return $new_string;

}



/**

 * Inserts strings into the $old_string at specified locations. Works the same way that str_insert()<br>

 * does except it allows you to insert multiple strings into multiple locations at the same time.

 *

 * @author  protokol

 * @version 1.0

 * @date    Thu Dec 02 15:00:16 EST 2004

 * @param   array $new_strings The strings to insert into the $old_string, along with the positions to insert them

 * @param   string $old_string The string that we want to manipulate

 * @return  string The newly created string

 */


function str_multi_insert ($new_strings, $old_string)

{

    // Since this function needs an array of new strings, just return the old string if we don't have it

    if (!is_array($new_strings)) {

        return $old_string;

    }

   

    $start = 0;

    $new_string = '';

   

    // Sort the input array so that we get correct substring indices

    ksort($new_strings);

   

    // Run through all of the insert values and create a new string with them

    foreach ($new_strings as $strpos => $insert_string) {

        // Get the section of the string from the old string

        $new_string .= substr($old_string, $start, ($strpos - $start));

       

        // Tack on the new string we want to insert

        $new_string .= $insert_string;

       

        // Set the new start position to the current string position

        $start = $strpos;

    }

   

    // Get the rest of the original string

    $new_string .= substr($old_string, $start);

   

    return $new_string;

}

?>


Below is an example of how you can use the functions:

Syntax: [ Download ] [ Hide ]
<?php

$string = 'I am a PHP developer.';

$new_string = str_insert(' kickass', 6, $string);



// This gives $new_string the value of:

// I am a kickass PHP developer.



$string = "8007233288";

$insert_strings = array(

    0 => '(',

    3 => ') ',

    6 => '-'

);

$new_string = str_multi_insert($insert_strings, $string);



// This gives $new_string the value of:

// (800) 723-3288

?>


That's all, so I hope you find these useful. Enjoy!


Last edited by protokol on Sat Jan 08, 2005 11:22 am, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 3:55 pm 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
I hope you don't mind but I edited the topic title slightly from "Useful String Insertion Functions" to "Useful String Functions". I was hoping we'd get a range of posts with general string tools.

Here's another.

Syntax: [ Download ] [ Hide ]
/*

    CLASS StringCutter



    Cut chunks out of a string using regex patterns.

    Can make repeated cuts on the same target string.

    The target string will remain unaffected.    

    Each cut removes a portion of the internal copy of the target string; the

    remainder can be obtained with the getRemainder() method. This could

    be used for example to check if the cuts have captured all data contained

    in the string.



*/


class StringCutter

{

    var $_num_cuts = 0;

    var $_num_cuts_error = 'The string cutter pattern has multiple matches.';

   

    var $cut;

    var $string;



    function StringCutter($string)

    {

        $this->string = $string;

    }



    function cut($regex)

    {

        $this->_num_cuts = 0;

        $this->cut = null;

        if($string = preg_replace_callback($regex,

                                           array(&$this, '_callback'),

                                           $this->string))

        {

            $this->string = $string;

        }

        if($this->_num_cuts > 1)

        {

            trigger_error($this->_num_cuts_error);

            return false;

        }

        return $this->cut;

    }

   

    function getRemainder()

    {

        return $this->string;

    }

   

    //////////////////////////////////////////

    //              PRIVATE                 //

    //////////////////////////////////////////

   

    /*

        Only looking at $ 0.

    */


    function _callback($match)

    {

        $this->cut = $match[0];

        $this->_num_cuts++;

        return '';

    }

}


The test (using ):

Syntax: [ Download ] [ Hide ]
class TestOfStringCutter extends UnitTestCase

{

    function TestOfStringCutter()

    {

        $this->UnitTestCase();

    }

    function testSimpleCut()

    {        

        $string = 'The quick brown fox.';

        $scissors =& new StringCutter($string);

        $this->assertIdentical($scissors->cut('#The quick#'), 'The quick');

        $this->assertIdentical($scissors->getRemainder(), ' brown fox.');

    }

    function testCutWithNoMatch()

    {

        $string = 'The quick brown fox.';

        $scissors =& new StringCutter($string);

        $this->assertIdentical($scissors->cut('#foobar#'), null);

        $this->assertIdentical($scissors->getRemainder(), 'The quick brown fox.');

    }

    function testMultipleCuts()

    {        

        $string = 'The quick brown fox.';

        $scissors =& new StringCutter($string);

        $this->assertIdentical($scissors->cut('#The quick#'), 'The quick');

        $this->assertIdentical($scissors->getRemainder(), ' brown fox.');

        $this->assertIdentical($scissors->cut('#foobar#'), null);

        $this->assertIdentical($scissors->getRemainder(), ' brown fox.');

        $this->assertIdentical($scissors->cut('#brown#'), 'brown');

        $this->assertIdentical($scissors->getRemainder(), '  fox.');

        $this->assertIdentical($scissors->cut('#foobar#'), null);

        $this->assertIdentical($scissors->getRemainder(), '  fox.');

    }

    function testOriginalStringIsUnmolested()

    {        

        $string = 'The quick brown fox.';

        $scissors =& new StringCutter($string);

        $scissors->cut('#The quick#');

        $this->assertIdentical($string, 'The quick brown fox.');

    }

    /*

        The class was written as part of a system to clean up user input where

        multiple bits of data had been stored in single fields. Multiple matches

        would mean the regex pattern has failed to identify a discrete data

        chunk; hence, an error is triggered if more than one match is found.

    */


    function testMultipleMatchesError()

    {        

        $string = 'The quick brown fox. The quick brown fox.';

        $scissors =& new StringCutter($string);

        $this->assertIdentical($scissors->cut('#The quick#'), false);

        $this->assertError($scissors->_num_cuts_error);

    }

    function testWithRegexError()

    {        

        $string = 'The quick brown fox.';

        $scissors =& new StringCutter($string);

        $this->assertIdentical($scissors->cut('quick'), null);

        $this->assertIdentical($scissors->getRemainder(), 'The quick brown fox.');

        $this->assertError('Delimiter must not be alphanumeric or backslash');

    }


Last edited by McGruff on Sun Aug 07, 2005 9:19 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Dec 03, 2004 5:35 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 5:17 pm 
Offline
Forum Regular

Joined: Mon Oct 18, 2004 8:14 pm
Posts: 741
php5 has some nice string functions not available in php4.

you can emulate the functionality w/ this package

http://pear.php.net/package/PHP_Compat

keep in mind, each function is kept in its own file,
so if you only need 1 or 2 functions,
you can just grab those files and include them manually
instead of installing the entire package


also, i dont think too many people know of these functions, thought id point them out

http://php.net/ctype


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 11, 2005 1:26 pm 
Offline
Forum Contributor

Joined: Fri Jun 18, 2004 3:04 am
Posts: 120
Syntax: [ Download ] [ Hide ]
function in_string($haystack, $needle, $insensitive = 0) {

  $func = $insensitive ? "stristr" : "strpos";

  return $func($haystack, $needle)!==false ? true : false;

}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 19, 2005 3:35 pm 
Offline
Forum Contributor
User avatar

Joined: Fri Jun 21, 2002 7:00 pm
Posts: 353
Location: Cleveland, OH
Syntax: [ Download ] [ Hide ]
/*

Split a string into two parts by giving it the index to perform the split at.

*/


function str_split_index($string, $index)

{

    $string = (string) $string;

    $index = (int) $index;



    $prefix = substr($string, 0, $index);

    $suffix = substr($string, $index);



    return array($prefix, $suffix);

}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 20, 2005 11:50 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 7:33 am 
Offline
Forum Contributor
User avatar

Joined: Fri Jun 21, 2002 7:00 pm
Posts: 353
Location: Cleveland, OH


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group