Skip to content

Commit 112e799

Browse files
committed
Flux 0.3.0
- Improves documentation with phone/date examples - Adds the letters() method - Renames the numbers() method to digits() - Adds support for quantifiers for digits() - Adds ignoreCase() and promotes it above inAnyCase() - Improves the documented API
1 parent e179227 commit 112e799

File tree

3 files changed

+195
-37
lines changed

3 files changed

+195
-37
lines changed

Flux.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public function startOfLine() { return $this->addPrefix( '^' ); }
105105

106106
public function endOfLine() { return $this->addSuffix( '$' ); }
107107

108-
public function inAnyCase() { return $this->addModifier('i'); }
108+
public function ignoreCase() { return $this->addModifier('i'); }
109+
public function inAnyCase() { return $this->ignoreCase(); }
109110

110111
public function searchOneLine() { return $this->removeModifier('m'); }
111112

@@ -148,9 +149,39 @@ public function anythingBut( $val )
148149
return $this->add( sprintf( '([^%s]*)', $this->sanitize( $val ) ) );
149150
}
150151

151-
public function word() { return $this->add( '\\w+' ); }
152+
public function word() { return $this->add( '(\\w+)' ); }
152153

153-
public function number() { return $this->add( '\\d+' ); }
154+
public function letters( $min=null, $max=null )
155+
{
156+
if ( $min && $max )
157+
{
158+
return $this->add( sprintf( '([a-zA-Z]{%d,%d})', $min, $max ) );
159+
}
160+
elseif ( $min && is_null($max) )
161+
{
162+
return $this->add( sprintf( '([a-zA-Z]{%d})', $min ) );
163+
}
164+
else
165+
{
166+
return $this->add( '([a-zA-Z]+)' );
167+
}
168+
}
169+
170+
public function digits( $min=null, $max=null )
171+
{
172+
if ( $min && $max )
173+
{
174+
return $this->add( sprintf( '(\\d{%d,%d})', $min, $max ) );
175+
}
176+
elseif ( $min && is_null($max) )
177+
{
178+
return $this->add( sprintf( '(\\d{%d})', $min ) );
179+
}
180+
else
181+
{
182+
return $this->add( '(\\d+)' );
183+
}
184+
}
154185

155186
public function orTry( $val )
156187
{

README.md

Lines changed: 113 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
## FLUX (Fluent Regular Expressions)
1+
## FLUX (Fluent Regular Expressions) 0.3.0
22
*by* [Selvin Ortiz](http://twitter.com/selvinortiz)
33

44
### Description
55
Fluent Regular Expressions _for_ PHP
66

77
----
88

9-
### Changelog
9+
### @Changelog
10+
11+
----
12+
#### 0.3.0
13+
- Improves documentation with `phone/date` examples
14+
- Adds the `letters()` method
15+
- Renames the `numbers()` method to `digits()`
16+
- Adds support for quantifiers for `digits()`
17+
- Adds `ignoreCase()` and promotes it above `inAnyCase()`
18+
- Improves the documented API
19+
20+
*_Thought hard about changing the name to `FluentX' any thoughts?_
1021

1122
----
1223
#### 0.2.0
@@ -20,13 +31,25 @@ Initial preview release
2031

2132
----
2233

23-
### Example
24-
----
34+
### @Todo
35+
- Add source code comments
36+
- Add support for quantifiers
37+
- Add language methods for more advanced use cases
38+
- Add support for array/array replacements
39+
- Add reference to repos that have port `FLUX`
40+
- Add license notes
41+
- Add contributing notes
42+
- Add credits
43+
44+
### @Examples
2545

2646
```php
27-
$flux = new Flux();
28-
$subject = 'http://selvinortiz.com';
47+
/**
48+
* Build a URL pattern then test w/ match() and do a replace()
49+
*/
2950

51+
$url = 'http://www.selvinortiz.com';
52+
$flux = new Flux();
3053
$flux
3154
->startOfLine()
3255
->find('http')
@@ -38,14 +61,51 @@ $flux
3861
->inAnyCase()
3962
->endOfLine();
4063

41-
// Echoing the instance will yield the compiled pattern (__toString)
42-
echo $flux; // /^(http)(s)?(\:\/\/)(www\.)?([^\.]*)(.in|.co|.com)$/i
64+
// Pattern /^(http)(s)?(\:\/\/)(www\.)?([^\.]*)(.in|.co|.com)$/i
65+
echo $flux->match( $url ); // true
66+
echo $flux->replace( 'https://$5$6', $url ); // https://selvinortiz.com
67+
68+
/**
69+
* Build a US Date pattern then test w/ match() and do a replace()
70+
*/
4371

44-
// Match against the subject string
45-
echo $flux->match( $subject ); // TRUE
72+
$date = 'Monday, Jul 22, 2013';
73+
$flux = new Flux();
74+
$flux
75+
->startOfLine()
76+
->word()
77+
->then(', ')
78+
->letters(3)
79+
->then(' ')
80+
->digits(1, 2)
81+
->then(', ')
82+
->digits(4)
83+
->endOfLine();
84+
85+
// Pattern /^(\()(\d{3})(\))( )?(\d{3})([ \-])(\d{4})$/
86+
echo $flux->match( $date ) ? 'matched' : 'unmatched'; // matched
87+
echo $flux->replace( '$3/$5/$7', $date ); // 612.424.0013
88+
89+
/**
90+
* Build a US Phone Number pattern then test w/ match() and do a replace()
91+
*/
92+
93+
$phone = '(612) 424-0013';
94+
$flux = new Flux();
95+
$flux
96+
->startOfLine()
97+
->find('(')
98+
->digits(3)
99+
->then(')')
100+
->maybe(' ')
101+
->digits(3)
102+
->anyOf(' -')
103+
->digits(4)
104+
->endOfLine();
46105

47-
// Replace the subject with matched segment $5 and $6
48-
echo $flux->replace( '$5$6', $subject ); // selvinortiz.com
106+
// Pattern /^(\()(\d{3})(\))( )?(\d{3})([ \-])(\d{4})$/
107+
echo $flux->match( $phone ) ? 'matched' : 'unmatched'; // matched
108+
echo $flux->replace( '$2.$5.$7', $phone ); // 612.424.0013
49109
```
50110

51111
### FLUX API
@@ -58,23 +118,54 @@ Adds a beginning of line `^` modifier
58118
Adds an end of line `$` modifier
59119

60120
#### `find( $val )`
61-
The first `segment` in the pattern, also an alias to `then( $val )`
62-
63121
#### `then( $val )`
64-
Allows you to augment the pattern with a required segment and like `find()`, it escapes regular expression chars
122+
Allow you to augment the pattern with a required `segment` and it escapes regular expression chars
65123

66124
#### `maybe( $val )`
67-
Allows you to augment the pattern with an optional segment
125+
Allows you to augment the pattern with an optional `segment`
126+
127+
#### `any( $val )`
128+
#### `anyOf( $val )`
129+
Creates a character class behind the scenes so that you can optionally match different chars
130+
131+
#### `anything()`
132+
Adds a *wild card* `(.*)` `segment` to the pattern but it does not make `dotAll()` explicit
133+
134+
#### `anythingBut( $val )`
135+
Will match anything but the chars in `$val` which is opposite of `any()` and `anyOf`
136+
137+
#### `word()`
138+
Adds `(\w+)` to the pattern which will match a single word
139+
140+
#### `letters( $min=null, $max=null )`
141+
Only matches chars in the alphabet and uses `$min` and `$max` to create a quantifier
142+
143+
#### `digits( $mix=null, $max=null )
144+
Only matches digits and uses `$min` and `$max` to create a quantifier like `word()`
145+
146+
#### `range( $from, $to [, $from, $to ...])`
147+
Allows you to create a `range` character class like `a-z0-9` by calling `range('a', 'z', 0, 9)`
148+
149+
#### `orTry()`
150+
This is experimental and I don't have the implementation I feel comfortable with...
151+
152+
#### `ignoreCase()`
153+
#### `inAnyCase()`
154+
Adds the `i` modifier to the pattern which will allow you to match in a case insensitive manner
155+
156+
#### `dotAll()`
157+
Adds the `s` modifier to the pattern which will allow you to match a `new line` when using `anything()`
68158

69-
#### `any()`
70-
Adds a *wild card* `(.*)` segment to the pattern but it does not make `dotAll()` explicit
159+
#### `multiline()`
160+
Adds the `m` modifier to the pattern which will allow you to search across multiple lines
71161

72-
#### `anyOf()`
73-
Alias to `any()`
162+
#### `searchOneLine()`
163+
Removes the modifier added by `multiline()` if it was previously called
74164

75165
#### `match( $subject )`
166+
Simply takes your `$subject` in, compares it against the pattern, and returns whether a it matched or not
76167

77168
#### `replace( $replacement, $subject )`
169+
You can replace matched `segments` by using the `$x` format where `x` is the `(int)` position of the matched `segment`
78170

79-
#### `(...)`
80-
There are plenty of other methods to document but I'll get to the rest shortly.
171+
### (...)

example.php

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,60 @@
11
<?php
22
require __DIR__.'/Flux.php';
33

4-
$flux = new Flux();
5-
$subject = 'http://selvinortiz.com';
6-
4+
$url = 'http://www.selvinortiz.com';
5+
$flux = new Flux();
76
$flux
87
->startOfLine()
9-
->then('http') // $1
10-
->maybe('s') // $2
11-
->then('://') // $3
12-
->maybe('www.') // $4
13-
->anythingBut('.') // $5
14-
->either('.in', '.co', '.com') // $6
8+
->find('http')
9+
->maybe('s')
10+
->then('://')
11+
->maybe('www.')
12+
->anythingBut('.')
13+
->either('.in', '.co', '.com')
1514
->inAnyCase()
1615
->endOfLine();
1716

18-
dd( $flux.'', false );
17+
dd( $flux, false );
18+
// Pattern /^(http)(s)?(\:\/\/)(www\.)?([^\.]*)(.in|.co|.com)$/i
19+
echo $flux->match( $url ) ? 'matched' : 'unmatched'; // matched
20+
echo $flux->replace( 'https://$5$6', $url ); // https://selvinortiz.com
21+
22+
$phone = '(612) 424-0013';
23+
$flux = new Flux();
24+
$flux
25+
->startOfLine()
26+
->find('(')
27+
->digits(3)
28+
->then(')')
29+
->maybe(' ')
30+
->digits(3)
31+
->anyOf(' -')
32+
->digits(4)
33+
->endOfLine();
34+
35+
dd( $flux, false );
36+
// Pattern /^(\()(\d{3})(\))( )?(\d{3})([ \-])(\d{4})$/
37+
echo $flux->match( $phone ) ? 'matched' : 'unmatched'; // matched
38+
echo $flux->replace( '$2.$5.$7', $phone ); // 612.424.0013
39+
40+
41+
$date = 'Monday, Jul 22, 2013';
42+
$flux = new Flux();
43+
$flux
44+
->startOfLine()
45+
->word()
46+
->then(', ')
47+
->letters(3)
48+
->then(' ')
49+
->digits(1, 2)
50+
->then(', ')
51+
->digits(4)
52+
->endOfLine();
1953

20-
dd( $flux->match( $subject) ? 'Matched' : 'Unmatched', false );
21-
dd( $flux->replace( '$5$6', $subject ) );
54+
dd( $flux, false );
55+
// Pattern /^(\()(\d{3})(\))( )?(\d{3})([ \-])(\d{4})$/
56+
echo $flux->match( $date ) ? 'matched' : 'unmatched'; // matched
57+
echo $flux->replace( '$3/$5/$7', $date ); // 612.424.0013
2258

2359
//--------------------------------------------------------------------------------
2460

0 commit comments

Comments
 (0)