PHP/String/ereg — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:07, 26 мая 2010
Содержание
break $url down into three distinct pieces
<?php
$url = "http://www.wbex.ru";
$parts = ereg("^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)", $url, $regs);
echo $regs[0];
echo "<br>";
echo $regs[1];
echo "<br>";
echo $regs[2];
echo "<br>";
echo $regs[3];
?>
Capturing Text Inside HTML or XML Tags
<?php
$text = "<p>This is some text here \"</p>\".</p>";
ereg("<p>(([^<\"]|[^<]*<[^\/][^<])*(\"[^\"]*\"([^<\"]|[^<]*<[^\/][^<])*)*)?<\/p>", $text, $matches);
echo "Found text: " . $matches[1] . "\n";
?>
Displaying elements of $regs array
<?
$url = "http://www.wbex.ru";
$www_url = ereg("^(http://www)\.([[:alnum:]]+)\.([[:alnum:]]+)", $url, $regs);
if ($www_url) :
echo $regs[0];
print "<br>";
echo $regs[1];
print "<br>";
echo $regs[2];
print "<br>";
echo $regs[3];
endif;
?>
ereg() function searches a string, returning true if the pattern is found, and false otherwise.
Its syntax is: int ereg(string pattern, string string, [array regs])
The search is case sensitive in regard to alphabetical characters.
Use ereg() to search strings for .ru domains:
<?
$email="a@a.ru";
$is_com = ereg("(\.)(com$)", $email);
print $is_com;
?>
ereg.php
<?php
$username = "Test";
if (ereg("([^a-z])",$username)) echo "Username must be all lowercase!";
?>
Filling Patterns with ereg
<?php
$s = "m@t.ca";
if (ereg ("([[:alpha:]]+)@([[:alpha:]]+)\.([[:alpha:]]{2,4})", $s, $matches))
{
echo "Regular expression successful. Dumping matches\n";
var_dump ($matches);
}
else
{
echo "Regular expression unsuccessful.\n";
}
?>