Приглашаем посетить
Ломоносов (lomonosov.niv.ru)

C.12 Appendix B

Previous Table of Contents Next

C.12 Appendix B

C.12.1 Exercise 1:

The regular expression ^\(?\d{3}\)?[- \.]\d{3}[- \.]\d{4}$ matches "an optional literal (, then three digits, then an optional literal ), then either a hyphen, space, or period, then three digits, then either a hyphen, space, or period, then four digits." The ^ and $ anchors make the expression match only phone numbers, not larger strings that contain phone numbers.

C.12.2 Exercise 2:

if (! preg_match('/^[a-z0-9]$/i', $_POST['username'])) {

    $errors[  ] = "Usernames must contain only letters or numbers.";

}

C.12.3 Exercise 3:

$zip = 98052;

$url = 'http://www.srh.noaa.gov/zipcity.php?inputstring=' . $zip;

$weather_page = file_get_contents($url);

if (preg_match('@<br><br>(-?\d+)&deg;F<br>\((-?\d+)&deg;C\)</td>@',

$weather_page,$matches)) {

    // $matches[1] is the Fahrenheit temp

    // $matches[2] is the Celsius temp

    print "The current temperature is $matches[1] degrees.";

} else {

    print "Can't get current temperature.";

}

C.12.4 Exercise 4:

$url = 'http://www.sklar.com/';

$page = file_get_contents($url);

if (preg_match_all('@<a href="[^"]+">.+?</a>@', $page, $matches)) {

    foreach ($matches[0] as $link) {

        print "$link <br/>\n";

    }

}

    Previous Table of Contents Next