I was a little scared myself, i didn't understand it. Most tutorials out there for regex aren't that great. They either go into to much detail and get confusing, or don't have enough info. However, i'm here to help you out, and set you on the right path. I doubt you believe this, but regex is alot different than just using the 'substr' function or any other string functions for that matter, simply because it kind of allows YOU to craft how to find what you're looking for. For instance, only with regex can you find every letter 'a' that comes after a ':'. Anyway, i've made a small code that searches parts of the 50 states of the usa, depending on if you're looking for it in the beginning, middle, or end. The code will explain itself. To understand the regex, read this small tutorial to understand what the symbols do. Don't worry, it'll take a few days for you to get it all, unless you're a natural. I wasn't.
Tutorial - Click Here
The form:
Code: Select all
<form method="post" action="eval.php">
Find: <input type=text name="find"> In:
<select name='what'>
<option>Beginning</option>
<option>Middle</option>
<option>End</option>
</select>
<input type='submit' value='Sort'>
</form>
Code: Select all
<pre>
<?php
$find = $_POST['find'];
$func = $_POST['what'];
$list = " Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky Louisiana Maine Maryland Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska Nevada NewHampshire NewJersey NewMexico NewYork NorthCarolina NorthDakota Ohio Oklahoma Oregon Pennsylvania RhodeIsland SouthCarolina SouthDakota Tennessee Texas Utah Vermont Virginia Washington WashingtonDC WestVirginia Wisconsin Wyoming ";
if($func == 'Beginning')
beginning();
if($func == 'Middle')
middle();
else
endd();
die();
function beginning()
{
global $find, $list;
$pat = "# ".$find."[a-zA-Z]+#";
preg_match_all($pat, $list, $match);
print_r($match);
}
function middle()
{
global $find, $list;
$pat = "#[a-zA-Z]+".$find."[a-zA-Z]+#";
preg_match_all($pat, $list, $match);
print_r($match);
}
function endd()
{
global $find, $list;
$pat = "#[A-Za-z]+".$find." #";
preg_match_all($pat, $list, $match);
print_r($match);
}
?>
On a side note, for those with experience, tell me what you think of this 'presentation', please.