|
użytkowników online: 40
|
OPINIE UŻYTKOWNIKÓW
|
Prawdziwa skarbnica wiedzy na temat tworzenia stron WWW i nie tylko. Korzystam z porad praktycznie codziennie, jest mi to niezbędne w mojej pracy. Sam zajmuję się tworzeniem serwisów, ale porady pisane przez Darka sa dla mnie nieocenioną pomocą! Proste, czytelne i zrozumiałe dla każdego! Czekam na więcej!
Krzysztof Szypulski
KESS - projektowanie stron
|
|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
preg_match_all (PHP 3 >= 3.0.9, PHP 4, PHP 5) preg_match_all -- Perform a global regular expression match Descriptionint preg_match_all ( string pattern, string subject, array &matches [, int flags [, int offset]] )
Searches subject for all matches to the regular
expression given in pattern and puts them in
matches in the order specified by
flags.
After the first match is found, the subsequent searches are continued
on from end of the last match.
flags can be a combination of the following flags
(note that it doesn't make sense to use
PREG_PATTERN_ORDER together with
PREG_SET_ORDER):
- PREG_PATTERN_ORDER
Orders results so that $matches[0] is an array of full
pattern matches, $matches[1] is an array of strings matched by
the first parenthesized subpattern, and so on.
- PREG_SET_ORDER
Orders results so that $matches[0] is an array of first set
of matches, $matches[1] is an array of second set of matches,
and so on.
In this case, $matches[0] is the first set of matches, and
$matches[0][0] has text matched by full pattern, $matches[0][1]
has text matched by first subpattern and so on. Similarly,
$matches[1] is the second set of matches, etc.
- PREG_OFFSET_CAPTURE
If this flag is passed, for every occurring match the appendant string
offset will also be returned. Note that this changes the return
value in an array where every element is an array consisting of the
matched string at offset 0 and its string offset
into subject at offset 1.
This flag is available since PHP 4.3.0 .
If no order flag is given, PREG_PATTERN_ORDER is
assumed.
Normally, the search starts from the beginning of the subject string. The
optional parameter offset can be used to specify
the alternate place from which to start the search.
The offset parameter is available since
PHP 4.3.3.
Notatka:
Using offset is not equivalent to
passing substr($subject, $offset) to
preg_match_all() in place of the subject string, because
pattern can contain assertions such as
^, $ or
(?<=x). See preg_match() for
examples.
Returns the number of full pattern matches (which might be zero),
or FALSE if an error occurred.
Przykład 1. Getting all phone numbers out of some text. |
<?php
preg_match_all("/\(? (\d{3})? \)? (?(1) [\-\s] ) \d{3}-\d{4}/x",
"Call 555-1212 or 1-800-555-1212", $phones);
?>
|
|
Przykład 2. Find matching HTML tags (greedy) |
<?php
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: " . $matches[0][$i] . "\n";
echo "part 1: " . $matches[1][$i] . "\n";
echo "part 2: " . $matches[3][$i] . "\n";
echo "part 3: " . $matches[4][$i] . "\n\n";
}
?>
|
This example will produce:
matched: <b>bold text</b>
part 1: <b>
part 2: bold text
part 3: </b>
matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: click me
part 3: </a> |
|
See also preg_match(),
preg_replace(),
and preg_split().
User Contributed Notesegingell at sisna dot com
01-Feb-2006 04:31
Try this for preg_match_all that takes an array of reg expers.
<?
function preg_search($ary, $subj) {
$matched = array();
if (is_array($ary)) {
foreach ($ary as $v) {
preg_match_all($v, $subj, $matched[]);
}
} else {
preg_match_all($ary, $subj, $matched[]);
}
return $matched;
}
?>
18-Dec-2005 03:16
Two match all occurrences between and including any two HTML tags, here <tr> and </tr>
preg_match_all("/(\<[ \\n\\r\\t]{0,}tr[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}tr[^>]*\>){1}
([^<]*<([^(\/>)]*(\/[^(t>)]){0,1}(\/t[^(r>)]){0,1})*>)*
(\<[ \\n\\r\\t]{0,}\/tr[^>]*\>|\<[^>]*[\\n\\r\\t]{1,}\/tr[^>]*\>){1}/i", $string, $Matches);
php at projectjj dot com
09-Dec-2005 09:43
Re: webmaster at swirldrop dot com
If you want to get a string with all the 'normal' characters, this may be better:
$clean = preg_replace('/\W+/', '', $dirty);
\W is the opposite of \w and will match any character that is not a letter or digit or the underscore character, plus it respects the current locale. Use [^0-9a-zA-Z]+ instead of \W if you need ASCII-only.
htp
07-Dec-2005 10:29
Just a quick note regarding the post by webmaster at swirldrop dot com. The regex doesn't match alpha-numerics, as it doesn't actually match numerics, just alphas. Might want to a add a 0-9 if that was the intend.
pablo dot seb at gmail dot com
16-Jun-2005 03:48
By assigning a name to a capturing group, you can easily reference it by name. (?P<name>group) captures the match of group into the backreference "name". You can reference the contents of the group with the numbered backreference or the named backreference
<?php
preg_match_all('|(a)(?P<x>b)(?P<y>c)(d)|','abcdefgabcdefg',$sub);
echo $sub[2][0]; echo '<br />';
echo $sub['y'][0]; ?>
Pablo from Salto, Uruguay
webmaster at m-bread dot com
07-Jun-2005 03:45
Looking at the function from rickyale at ig dot com dot br below getting URLs from an html file, I think this is slightly better:
function get_urls($string, $strict=true) {
$types = array("href", "src", "url");
while(list(,$type) = each($types)) {
$innerT = $strict?'[a-z0-9:?=&@/._-]+?':'.+?';
preg_match_all ("|$type\=([\"'`])(".$innerT.")\\1|i", $string, &$matches);
$ret[$type] = $matches[2];
}
return $ret;
};
This only gets urls in quotes "...", `...` or '...', but not mixed quotes like `..." (thanks to w w w's note on the 'pattern syntax' page). If you set the second parameter to false, then the function will give you any contents of attribute (so the function can be used to get other attributes, such as alt). To make it more strict, the '[a-z0-9:?=&@/._-]+?' can be replaced with a regular expression for a url.
webmaster at swirldrop dot com
07-Jun-2005 02:40
If you want to get al the text characters from a string, possibly entered by a user, and filter out all the non alpha-numeric characters (perhaps to make an ID to enter user-submitted details into a database record), then you can use the function below. It returns a string of only the alpha-numeric characters from the input string (all in lower case), with all other chracters removed:
<?php
function getText($string){
preg_match_all('/(?:([a-z]+)|.)/i', $string, $matches);
return strtolower(implode('', $matches[1]));
};?>
It took me quite a while tocome up with this regular expression. I hope it saves someone else that time.
20-Apr-2005 05:35
A little correction to my function below:
<?php
function urlhighlight($str) {
preg_match_all("/http:\/\/?[^ ][^<]+/i",$str,$lnk);
$size = sizeof($lnk[0]);
$i = 0;
while ($i < $size) {
$len = strlen($lnk[0][$i]);
if($len > 30) {
$lnk_txt = substr($lnk[0][$i],0,30)."...";
} else {
$lnk_txt = $lnk[0][$i];
}
$ahref = $lnk[0][$i];
$str = str_replace($ahref,"<a href='$ahref' target='_blank'>$lnk_txt</a>",$str);
$i++;
}
return $str;
}
?>
The error is in the preg_match_all("/http:\/\/?[^ ][^<]+/i",$str,$lnk); the [^<] was missing.
Dan Madsen
20-Apr-2005 03:25
I wrote a function, which takes urls from a string, or database output, highlights them, and shortens the links name if its above 30 characters.
Note: You'll have to use nl2br() function on the string before using it, because I didn't know how to check for LineFeed or CarrigeReturn in preg-style.
<?php
function urlhighlight($str) {
preg_match_all("/http:\/\/?[^ ]+/i",$str,$lnk);
$size = sizeof($lnk[0]);
$i = 0;
while ($i < $size) {
$len = strlen($lnk[0][$i]);
if($len > 30) {
$lnk_txt = substr($lnk[0][$i],0,30)."...";
} else {
$lnk_txt = $lnk[0][$i];
}
$ahref = $lnk[0][$i];
$str = str_replace($ahref,"<a href='$ahref'>$lnk_txt</a>",$str);
$i++;
}
return $str;
}
?>
Ex:
<?php
$str = "a lot of text with urls in it and alot of linebreaks";
$str = urlhighlight(nl2br($str));
?>
b2sing4u at naver dot com
09-Apr-2005 12:42
This function converts all HTML style decimal character code to hexadecimal code.
ex) Hi ο ◊ Dec -> Hi ο ◊ Dec
function d2h($word) {
$n = preg_match_all("/&#(\d+?);/", $word, $match, PREG_PATTERN_ORDER);
for ($j = 0; $j < $n; $j++) {
$word = str_replace($match[0][$j], sprintf("&#x%04X;", $match[1][$j]), $word);
}
return($word);
}
& This function converts all HTML style hexadecimal character code to decimal code.
ex) Hello ο ◊ Hex -> Hello ο ◊ Hex
function h2d($word) {
$n = preg_match_all("/&#x([0-9a-fA-F]+?);/", $word, $match, PREG_PATTERN_ORDER);
for ($j = 0; $j < $n; $j++) {
$word = str_replace($match[0][$j], sprintf("&#%u;", hexdec($match[1][$j])), $word);
}
return($word);
}
b2sing4u
07-Apr-2005 11:24
Character Code Conversion Example.
You can use following example to convert character code in HTML file.
First example converts Hexadecimal code to Decimal code.
ex) Hello ÿ Hex -> Hello ÿ Hex
Second example converts Decimal code to Hexadecimal code.
ex) Hi  Dec -> Hi  Dec
<?php
$h2d_get = fopen("h2d_get.htm", 'r');
$h2d_out = fopen("h2d_out.htm", 'w');
for ($i = 1; $i <= 1000; $i++)
{
if (feof($h2d_get)) { break; }
$line = fgets($h2d_get, 409600);
$line = trim($line);
if ($line == "99999999") { break; }
$n = preg_match_all("/&#x([0-9a-fA-F]+?);/", $line, $match, PREG_PATTERN_ORDER);
for ($j = 0; $j < $n; $j++)
{
$find = $match[0][$j];
$code = hexdec($match[1][$j]);
$push = sprintf("&#%u;", $code);
$line = eregi_replace($find, $push, $line);
}
fwrite($h2d_out, $line);
fwrite($h2d_out, "\r\n");
}
fclose($h2d_get);
fclose($h2d_out);
?>
<?php
$d2h_get = fopen("d2h_get.htm", 'r');
$d2h_out = fopen("d2h_out.htm", 'w');
for ($i = 1; $i <= 1000; $i++)
{
if (feof($d2h_get)) { break; }
$line = fgets($d2h_get, 409600);
$line = trim($line);
if ($line == "99999999") { break; }
$n = preg_match_all("/&#(\d+?);/", $line, $match, PREG_PATTERN_ORDER);
for ($j = 0; $j < $n; $j++)
{
$find = $match[0][$j];
$code = $match[1][$j];
$push = sprintf("&#x%04X;", $code);
$line = eregi_replace($find, $push, $line);
}
fwrite($d2h_out, $line);
fwrite($d2h_out, "\r\n");
}
fclose($d2h_get);
fclose($d2h_out);
?>
arias at elleondeoro dot com
15-Feb-2005 01:27
If you want to find all positions and his length, you can use the next function:
<?php
function preg_match_all_positions($pattern, $subject, &$count=null, $flags=0, $offset=0) {
for ($count=0; preg_match($pattern, $subject, $match, $flags, $offset); $count++) {
$positions[0][] = $pos = strpos($subject, $match[0], $offset);
$positions[1][] = $len = strlen($match[0]);
$offset = $pos+$len;
}
return $positions;
}
?>
mpbweb at mbourque dot com
02-Feb-2005 07:41
Here is a handy function I wrote that will check for broken links on the supplied url.
function dead_links($url) {
// mixed link_checker( $url )
// Returns:
// FALSE if no broken links are found.
// ARRAY containing broken links if any are found.
ob_start();
if( !readfile($url) ) return FALSE;
$body = ob_get_contents();
ob_end_clean();
$pathparts = pathinfo($url);
$urlpattern = "/<a[^>]+href=\"([^\"]+)/i";
preg_match_all($urlpattern,$body,$matches);
foreach( $matches[1] as $link) {
if( strpos($link,"http://") === FALSE ) { // Deal with relative paths
$link = $pathparts['dirname'] . "/" . $link;
}
$fp = @fopen("$link", "r");
fclose($fp);
if (!$fp) {
$linkArray[] = $link;
}
}
return (is_array($linkArray) ) ? $linkArray : FALSE;
}
Regards,
Michael Bourque
MCLD
20-Jan-2005 11:35
Here's a nice easy use for preg_match_all. I have data files in comma-separated-values format, with all the data enclosed in quote marks. To convert one line of such a data file into an array:
function quotedCsvLineToArray($l)
{
preg_match_all('/(?<=,|\A)("(.*?)")?(?=,|\Z)/',$l, $matches, PREG_PATTERN_ORDER);
return $matches[2];
}
hope it helps
dan
hex6ng at yahoo dot com
03-Jul-2004 12:04
This is a much more efficient version of the same function posted in ereg_replace() discussion by hdn, who is the same person as hex6ng. I didn't include activating urls without http:// protocol identifier because there are many xxx.xxx patterns that are not urls.
function html_activate_urls($str)
{
// lift all links, images and image maps
$url_tags = array (
"'<a[^>]*>.*?</a>'si",
"'<map[^>]*>.*?</map>'si",
"'<script[^>]*>.*?</script>'si",
"'<style[^>]*>.*?</style>'si",
"'<[^>]+>'si"
);
foreach($url_tags as $url_tag)
{
preg_match_all($url_tag, $str, $matches, PREG_SET_ORDER);
foreach($matches as $match)
{
$key = "<" . md5($match[0]) . ">";
$search[] = $key;
$replace[] = $match[0];
}
}
$str = str_replace($replace, $search, $str);
// indicate where urls end if they have these trailing special chars
$sentinals = array("/&(quot|#34);/i", // Replace html entities
"/&(lt|#60);/i",
"/&(gt|#62);/i",
"/&(nbsp|#160);/i",
"/&(iexcl|#161);/i",
"/&(cent|#162);/i",
"/&(pound|#163);/i",
"/&(copy|#169);/i");
$str = preg_replace($sentinals, "<marker>\\0", $str);
// URL into links
$str =
preg_replace( "|\w{3,10}://[\w\.\-_]+(:\d+)?[^\s\"\'<>\(\)\{\}]*|",
"<a href=\"\\0\">\\0</a>", $str );
$str = str_replace("<marker>", '', $str);
return str_replace($search, $replace, $str);
}
-hdn
vb_user at yahoo dot com
22-Apr-2004 06:00
If you want to extract the list of php functions in one of your library (ie, includes) for documentation or any purpose use the below:
$filename = 'library.php';
$fp = fopen($filename,'r');
if ($fp !== false) {
$str = fread($fp, filesize ($filename));
$count = preg_match_all ("|function[ ]+(.*)[\(](.*)[\)]|U", $str, $out, PREG_PATTERN_ORDER);
for ($i=0; $i<$count; $i++) {
if (!eregi('array',$out[1][$i])) {
echo '#T='.$out[1][$i]."\n";
echo $out[1][$i].'('.$out[2][$i].')'."\n\n";
}
}
}
fabriceb at gmx dot net
05-Mar-2004 03:55
If you just want to find out how many times a string contains another simple string, don't use preg_match_all like I did before I fould the substr_count function.
Use
<?php
$nrMatches = substr_count ('foobarbar', 'bar');
?>
instead. Hope this helps some other people like me who like to think too complicated :-)
|