Passed
Pull Request — master (#130)
by Roberto
05:08
created

Strings::removeSomeAlienCharsfromTxt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace NFePHP\Common;
4
5
/**
6
 * Classe auxiliar para o tratamento de strings
7
 * @category   NFePHP
8
 * @package    NFePHP\Common\Strings
9
 * @copyright  Copyright (c) 2008-2017
10
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
11
 * @author     Roberto L. Machado <linux dot rlm at gmail dot com>
12
 * @link       http://github.com/nfephp-org/nfephp for the canonical source repository
13
 */
14
15
class Strings
16
{
17
    /**
18
     * Replace all specials characters from string and retuns only 128 basics
19
     * NOTE: only for UTF-8
20
     * @param string $string
21
     * @return  string
22
     */
23 1
    public static function replaceSpecialsChars($string)
24
    {
25 1
        $string = trim($string);
26 1
        $aFind = ['&','á','à','ã','â','é','ê','í','ó','ô','õ','ú','ü',
27
            'ç','Á','À','Ã','Â','É','Ê','Í','Ó','Ô','Õ','Ú','Ü','Ç'];
28 1
        $aSubs = ['e','a','a','a','a','e','e','i','o','o','o','u','u',
29
            'c','A','A','A','A','E','E','I','O','O','O','U','U','C'];
30 1
        $newstr = str_replace($aFind, $aSubs, $string);
31 1
        $newstr = preg_replace("/[^a-zA-Z0-9 @,-.;:\/]/", "", $newstr);
32 1
        return $newstr;
33
    }
34
    
35
    /**
36
     * Remove all non numeric characters from string
37
     * @param string $string
38
     * @return string
39
     */
40 1
    public static function onlyNumbers($string)
41
    {
42 1
        return preg_replace("/[^0-9]/", "", $string);
43
    }
44
    
45
    /**
46
     * Remove unwanted attributes, prefixes, sulfixes and other control
47
     * characters like \r \n \s \t
48
     * @param string $string
49
     * @param boolean $removeEncodingTag remove encoding tag from a xml
50
     * @return string
51
     */
52 2
    public static function clearXmlString($string, $removeEncodingTag = false)
53
    {
54
        $aFind = array(
55 2
            'xmlns:default="http://www.w3.org/2000/09/xmldsig#"',
56
            ' standalone="no"',
57
            'default:',
58
            ':default',
59
            "\n",
60
            "\r",
61
            "\t"
62
        );
63 2
        $retXml = str_replace($aFind, "", $string);
64 2
        $retXml = preg_replace('/(\>)\s*(\<)/m', '$1$2', $retXml);
65 2
        if ($removeEncodingTag) {
66 1
            $retXml = self::deleteAllBetween($retXml, '<?xml', '?>');
67
        }
68 2
        return $retXml;
69
    }
70
    
71
    /**
72
     * Remove all characters between markers
73
     * @param string $string
74
     * @param string $beginning
75
     * @param string $end
76
     * @return string
77
     */
78 2
    public static function deleteAllBetween($string, $beginning, $end)
79
    {
80 2
        $beginningPos = strpos($string, $beginning);
81 2
        $endPos = strpos($string, $end);
82 2
        if ($beginningPos === false || $endPos === false) {
83
            return $string;
84
        }
85 2
        $textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);
86 2
        return str_replace($textToDelete, '', $string);
87
    }
88
    
89
    /**
90
     * Clears the xml after adding the protocol, removing repeated namespaces
91
     * @param string $string
92
     * @return string
93
     */
94 1
    public static function clearProtocoledXML($string)
95
    {
96 1
        $procXML = self::clearXmlString($string);
97 1
        $aApp = array('nfe','cte','mdfe');
98 1
        foreach ($aApp as $app) {
99 1
            $procXML = str_replace(
100 1
                'xmlns="http://www.portalfiscal.inf.br/'.$app.'" xmlns="http://www.w3.org/2000/09/xmldsig#"',
101 1
                'xmlns="http://www.portalfiscal.inf.br/'.$app.'"',
102
                $procXML
103
            );
104
        }
105 1
        return $procXML;
106
    }
107
    
108
    /**
109
     * Remove some alien chars from txt
110
     * @param string $txt
111
     * @return string
112
     */
113 1
    public static function removeSomeAlienCharsfromTxt($txt)
114
    {
115
        //remove CRs and TABs
116 1
        $txt = str_replace(["\r","\t"], "", $txt);
117
        //remove multiple spaces
118 1
        $txt = preg_replace('/(?:\s\s+)/', ' ', $txt);
119
        //remove spaces at begin and end of fields
120 1
        $txt = str_replace(["| "," |"], "|", $txt);
121 1
        return $txt;
122
    }
123
    
124
    /**
125
     * Creates a string ramdomically with the specified length
126
     * @param int $length
127
     * @return string
128
     */
129 1
    public static function randomString($length)
130
    {
131
        $keyspace = '0123456789abcdefghijklmnopqr'
132 1
            . 'stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
133 1
        $str = '';
134 1
        $max = mb_strlen($keyspace, '8bit') - 1;
135 1
        for ($i = 0; $i < $length; ++$i) {
136 1
            $str .= $keyspace[rand(0, $max)];
137
        }
138 1
        return $str;
139
    }
140
}
141