PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.6
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.6
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmStringReaderHelper.php

FrmStringReaderHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.6, at classes/helpers/FrmStringReaderHelper.php

124 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 die( 'You are not allowed to call this page directly.' );
5 }
6
7 /**
8 * Given a string, this class will read through the string using
9 * one of a number of terminating rules:
10 * - One character.
11 * - A specified number of characters.
12 * - Until a matching character is found.
13 *
14 * @since 6.2
15 */
16 class FrmStringReaderHelper {
17
18 /**
19 * @var int
20 */
21 private $pos = 0;
22
23 /**
24 * @var int
25 */
26 private $max = 0;
27
28 /**
29 * @var string
30 */
31 private $string;
32
33 /**
34 * @param string $string
35 */
36 public function __construct( $string ) {
37 // Split the string up into an array of UTF-8 characters.
38 // As an array we can read through it one character at a time.
39
40 $this->string = $string;
41 $this->max = strlen( $this->string ) - 1;
42 }
43
44 /**
45 * Read characters until we reach the given character $char.
46 * By default, discard that final matching character and return the rest.
47 *
48 * @param string $char
49 * @param bool $discard_char
50 * @return string
51 */
52 public function read_until( $char, $discard_char = true ) {
53 $value = '';
54
55 while ( null !== ( $one = $this->read_one() ) ) {
56 if ( $one !== $char || ! $discard_char ) {
57 $value .= $one;
58 }
59
60 if ( $one === $char ) {
61 break;
62 }
63 }
64
65 return $value;
66 }
67
68 /**
69 * Read $count characters, or until we have reached the end,
70 * whichever comes first.
71 * By default, remove enclosing double-quotes from the result.
72 *
73 * @param int $count
74 * @return string
75 */
76 public function read( $count ) {
77 $value = '';
78
79 while ( $count > 0 && ! is_null( $one = $this->read_one() ) ) {
80 $value .= $one;
81 --$count;
82 }
83
84 return $this->strip_quotes( $value );
85 }
86
87 /**
88 * Read the next character from the supplied string.
89 * Return null when we have run out of characters.
90 *
91 * @return string|null
92 */
93 private function read_one() {
94 if ( $this->pos <= $this->max ) {
95 $value = $this->string[ $this->pos ];
96 $this->pos += 1;
97 } else {
98 $value = null;
99 }
100 return $value;
101 }
102
103 /**
104 * Remove a single set of double-quotes from around a string.
105 * abc => abc
106 * "abc" => abc
107 * ""abc"" => "abc"
108 *
109 * @param string string
110 * @return string
111 */
112 private function strip_quotes( $string ) {
113 // Only remove exactly one quote from the start and the end and then only if there is one at each end.
114
115 if ( strlen( $string ) < 2 || substr( $string, 0, 1 ) !== '"' || substr( $string, -1, 1 ) !== '"' ) {
116 // Too short, or does not start or end with a quote.
117 return $string;
118 }
119
120 // Return the middle of the string, from the second character to the second-but-last.
121 return substr( $string, 1, -1 );
122 }
123 }
124