mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-16 08:58:56 +00:00
update preg_split() and preg_quote examples
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@29415 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
d5292588e7
commit
5d7cdfbcc5
1 changed files with 11 additions and 15 deletions
|
@ -474,13 +474,10 @@ $text = preg_replace ($search, $replace, $document);
|
|||
<example>
|
||||
<title>Getting parts of search string</title>
|
||||
<programlisting role="php">
|
||||
// split the phrase by any number of commas or space characters,
|
||||
// which include " ", \r, \t, \n and \f
|
||||
$keywords = preg_split ("/[\s,]+/", "hypertext language, programming");
|
||||
</programlisting>
|
||||
<para>
|
||||
This example splits the text by any number of spaces or
|
||||
commas. So the resulting $keywords array would be ("hypertext",
|
||||
"language", "programming")
|
||||
</para>
|
||||
</example>
|
||||
See also <function>preg_match</function>,
|
||||
<function>preg_match_all</function>, and
|
||||
|
@ -533,19 +530,17 @@ echo $keywords; // returns \$40 for a g3\/400
|
|||
<example>
|
||||
<title>Italicizing a word within some text</title>
|
||||
<programlisting role="php">
|
||||
|
||||
// In this example, preg_quote($word) is used to keep the
|
||||
// asterisks from having special meaning to the regular
|
||||
// expression.
|
||||
|
||||
$textbody = "This book is *very* difficult to find.";
|
||||
$word = "*very*";
|
||||
$textbody = preg_replace ("|\b".preg_quote($word)."\b|",
|
||||
$textbody = preg_replace ("/".preg_quote($word)."/",
|
||||
"<i>".$word."</i>",
|
||||
$textbody);
|
||||
</programlisting>
|
||||
<para>
|
||||
In this example, preg_quote($word) is used to keep the
|
||||
asterisks from having special meaning to the regular
|
||||
expression. The \b is recognized by pcre to find a word
|
||||
boundary, so only whole words match. This way something like
|
||||
"is*very*difficult" would not be considered a match.
|
||||
</para>
|
||||
</example>
|
||||
</para>
|
||||
</refsect1>
|
||||
|
@ -577,8 +572,9 @@ $textbody = preg_replace ("|\b".preg_quote($word)."\b|",
|
|||
<example>
|
||||
<title><function>preg_grep</function> example</title>
|
||||
<programlisting role="php">
|
||||
preg_grep ("/^(\d+)?\.\d+$/", $array); // find all array elements
|
||||
// containing floating point numbers
|
||||
// return all array elements
|
||||
// containing floating point numbers
|
||||
$fl_array = preg_grep ("/^(\d+)?\.\d+$/", $array);
|
||||
</programlisting>
|
||||
</example>
|
||||
</para>
|
||||
|
|
Loading…
Reference in a new issue