PHP/String/ereg

Материал из Web эксперт
Перейти к: навигация, поиск

break $url down into three distinct pieces

   <source lang="html4strict">

<?php

  $url = "http://www.wbex.ru";
  $parts = ereg("^(http://www)\.(alnum:+)\.(alnum:+)", $url, $regs);
  echo $regs[0];     
  echo "
"; echo $regs[1]; echo "
"; echo $regs[2]; echo "
"; echo $regs[3];

?>

 </source>
   
  


Capturing Text Inside HTML or XML Tags

   <source lang="html4strict">

<?php

$text = "

This is some text here \"

\".</p>"; ereg("

(([^<\"]|[^<]*<[^\/][^<])*(\"[^\"]*\"([^<\"]|[^<]*<[^\/][^<])*)*)?<\/p>", $text, $matches); echo "Found text: " . $matches[1] . "\n"; ?> </source>

Displaying elements of $regs array

   <source lang="html4strict">

<? $url = "http://www.wbex.ru"; $www_url = ereg("^(http://www)\.(alnum:+)\.(alnum:+)", $url, $regs); if ($www_url) :

    echo $regs[0];     
    print "
"; echo $regs[1]; print "
"; echo $regs[2]; print "
"; echo $regs[3];

endif; ?>

 </source>
   
  


ereg() function searches a string, returning true if the pattern is found, and false otherwise.

   <source lang="html4strict">

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;

?>

 </source>
   
  


ereg.php

   <source lang="html4strict">

<?php

  $username = "Test";
  if (ereg("([^a-z])",$username)) echo "Username must be all lowercase!";

?>

 </source>
   
  


Filling Patterns with ereg

   <source lang="html4strict">

<?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";
   }

?>

</source>