http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 2 LOC: 143 Statements: 39

Source file Statements Methods Total coverage
String.php 97.4% 100.0% 97.6%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * This source file is subject to the new BSD license that is bundled
6
 * with this package in the file LICENSE.txt.
7
 * It is also available through the world-wide-web at this URL:
8
 * http://www.opensource.org/licenses/bsd-license.php
9
 *
10
 * @category  Xyster
11
 * @package   Xyster
12
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id: String.php 199 2008-01-19 21:54:40Z doublecompile $
15
 */
16
/**
17
 * Helper class for string manipulation
18
 *
19
 * @category  Xyster
20
 * @package   Xyster
21
 * @copyright Copyright (c) 2007-2008 Irrational Logic (http://irrationallogic.net)
22
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
23
 */
24
class Xyster_String
25
{
26
    /**
27
     * Used for matching parenthesis groups not inside quotes
28
     *
29
     * It works, but this still matches parentheses inside quotes that aren't in
30
     * parentheses.
31
     */
32
    const PARENTH_QUOTE_REGEX = '/(?<![\w\d])(\(((?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"|[^()])|(?1))+\))/m';
33
34
    /**
35
     * Match nested parentheses groups not inside of double-quotes
36
     *
37
     * This method will find all balanced parentheses not inside of double
38
     * quotes.  It also respects escaped double quotes (i.e. with a preceding
39
     * backslash).
40
     *
41
     * For example, if you ran this method on a string containing JavaScript
42
     * code:
43
     * <code>
44
     * $string = <<<HEREDOC
45
     * var n = navigator;
46
     * return ( n.appName == "Netscape" ) || n.appVersion != "5.0 (Windows; en-US)";
47
     * HEREDOC;
48
     * </code>
49
     *
50
     * You would receive back an array containing:
51
     * <pre>
52
     * array (
53
     *     [0]=>( n.appName == "Netscape" )
54
     * );
55
     * </pre>
56
     *
57
     * The second matched parentheses are inside of quotation marks and thus
58
     * should be treated as part of that string literal.
59
     *
60
     * @param string $string
61
     * @return array
62
     * @todo make this work with top-level strings with parenths in them
63
     */
64
    static public function matchGroups( $string )
65
    {
66 55
        if ( strpos($string, '(') === false ) {
67 54
            return array();
68 0
        }
69
70 38
        $matches = array();
71 38
        preg_match_all(self::PARENTH_QUOTE_REGEX, $string, $matches, PREG_SET_ORDER);
72
73 38
        $groups = array();
74 38
        foreach( $matches as $group ) {
75 36
            $groups[] = $group[0];
76 36
        }
77 38
        return $groups;
78
    }
79
80
	/**
81
	 * Like explode(), but won't split inside of parentheses or double-quotes
82
	 *
83
	 * This method will split a string into an array using a string as a
84
	 * seperator.  If the seperator is contained within a pair of double-quotes
85
	 * or between matching parentheses, it will be ignored.  If the seperator is
86
	 * not found, the method will return an array with one element containing
87
	 * the entire string.
88
	 *
89
	 * Example:
90
	 * <code>
91
	 * $haystack = 'A (great (test)) of "this method"';
92
	 * print_r(Xyster_String::smartSplit(' ', $haystack));
93
	 * </code>
94
	 *
95
	 * Would print:
96
	 * <pre>
97
	 * array (
98
	 *    [0]=>A
99
	 * 	  [1]=>(great (test))
100
	 * 	  [2]=>of
101
	 * 	  [3]=>"this method"
102
	 * )
103
	 * </pre>
104
	 *
105
	 * @param string $needle  String used as seperator
106
	 * @param string $haystack  String to split
107
	 * @param bool $caseInsensitive  Whether to ignore case
108
	 * @return array  Split parts of $haystack
109
	 */
110
	static public function smartSplit( $needle, $haystack, $caseInsensitive=true )
111
	{
112 82
		$split = array();
113 82
		$buff = "";
114 82
		$inPar = 0;
115 82
		$inStr = false;
116 82
		for( $i=0; $i<strlen($haystack); $i++ ) {
117 82
			$currNeed = substr( $haystack, $i, strlen($needle) );
118 82
			$curr = $currNeed[0];
119 82
			$last = ( $i ) ? $haystack[$i-1] : "";
120 82
			if ( $curr == '"' && ( ( $inStr && $last != "\\") || !$inStr ) ) {
121 23
				$inStr = !$inStr;
122 23
			}
123 82
			if ( !$inStr ) {
124 82
				if ( $curr == "(" ) {
125 24
					$inPar++;
126 82
				} else if ( $curr == ")" ) {
127 24
					$inPar--;
128 24
				}
129 82
			}
130 82
			if ( !$inPar && !$inStr &&
131 82
				( $currNeed == $needle ||
132 82
				( $caseInsensitive && strcasecmp($currNeed,$needle) == 0 ) ) ) {
133 63
				$split[] = $buff;
134 63
				$buff = "";
135 63
				$i = $i+(strlen($needle)-1);
136 63
			} else {
137 82
				$buff .= $curr;
138
			}
139 82
		}
140 82
		$split[] = $buff;
141 82
		return $split;
142
	}
143
}


Report generated at 2008-01-20T12:13:39-05:00