PHP Classes

File: tests/conditional_pattern.php

Recommend this page to a friend!
  Classes of Marco Marchiņ   Regexp Builder   tests/conditional_pattern.php   Download  
File: tests/conditional_pattern.php
Role: Example script
Content type: text/plain
Description: Conditional pattern test
Class: Regexp Builder
Build regular expressions programmatically
Author: By
Last change:
Date: 14 years ago
Size: 1,015 bytes
 

Contents

Class file image Download
<?php
require_once "../regexpBuilder.php";
/*
Find any word that starts with "abc". If it's followed by a "d" then match the rest of the word otherwise match the first character after "abc"
String: "abcdefg abc2323 abcdd abc342"
LOGIC:
- start capture
- match "abc"
- start condition
- if followed by "d"
- then match every letter repeated one or more times
- otherwise match the first character
- close the condition
- end capture
*/

$regexp=new regexpBuilder();
$regexp->capture() //start capture
->match("abc") //match "abc"
->ifItIs(FOLLOWED_BY) //start condition
->match("d") //if followed by "d"
->then()->match(LETTER_CHAR)->frequency(ONE_OR_MORE) //then match every letter repeated one or more times
->otherwise()->matchEverything() //otherwise match the first character
->closeIf() //close the condition
->closeCapture(); //end capture

$match=$regexp->execOn("abcdefg abc2323 abcdd abc342");
foreach(
$match[1] as $result)
    echo
$result."<br>"; //abcdefg, abc2, abcdd, abc3
?>