PHP/String/strrpos
Содержание
- 1 A third parameter to strpos( ) that allows us to specify where to start searching from
- 2 Finding a substring with strpos()
- 3 Finding the Position of a Substring with strpos()
- 4 int strpos ( string haystack, mixed needle [, int offset] ) returns the index of the beginning of a substring"s first occurrence within a string
- 5 Return String found at position 22
- 6 strpos
- 7 strpos() function finds the position of the first occurrence in string.
- 8 strpos( ) will return false (as opposed to -1)
- 9 strrpos() function locates the last occurrence of character in string.
- 10 strrpos.php
- 11 Using several functions together to extract a portion of a string
- 12 Using String Matching vs. Pattern Matching
A third parameter to strpos( ) that allows us to specify where to start searching from
<source lang="html4strict">
<?
$string = "This is a strpos( ) test"; $pos = strpos($string, "i", 3); if ($pos = == false) { print "Not found\n"; } else { print "Found at $pos!\n"; }
?>
</source>
Finding a substring with strpos()
<source lang="html4strict">
<?php if (strpos($_POST["email"], "@") === false) {
print "There was no @ in the e-mail address!"; }
?>
</source>
Finding the Position of a Substring with strpos()
<source lang="html4strict">
<?php $membership = "asdfasdf"; if ( strpos($membership, "mz") === 0 ) {
print "hello mz";
} ?>
</source>
int strpos ( string haystack, mixed needle [, int offset] ) returns the index of the beginning of a substring"s first occurrence within a string
<source lang="html4strict">
<?
$string = "This is a strpos( ) test"; print strpos($string, "s") . "\n";
?>
</source>
Return String found at position 22
<source lang="html4strict">
<?php
$haystack = "this is a test. this is a test. this is a test"; $pos = strpos ($haystack, "bottle"); if ($pos === false) echo "String not found\n"; else echo "String found at position $pos\n";
?>
</source>
strpos
<source lang="html4strict">
<?php
$substr = "index.html"; $log = "192.168.1.11:/www/htdocs/index.html:"; $pos = strpos($log, $substr); echo "$pos";
?>
</source>
strpos() function finds the position of the first occurrence in string.
<source lang="html4strict">
Its syntax is: int strpos (string string, string occurrence [, int offset]) offset specifies the position at which to begin the search. <? $log = "1999-01-31"; $pos = strpos($log, "1999"); print $pos; ?>
</source>
strpos( ) will return false (as opposed to -1)
<source lang="html4strict">
<?
$string = "This is a strpos( ) test"; $pos = strpos($string, "This"); if ($pos = == false) { print "Not found\n"; } else { print "Found!\n"; }
?>
</source>
strrpos() function locates the last occurrence of character in string.
<source lang="html4strict">
Its syntax is: int strrpos (string string, char character) <?
print strrpos("asdf","d");
?>
</source>
strrpos.php
<source lang="html4strict">
<?php
$limit = 100;
$summary = <<< summary this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. summary;
if (strlen($summary) > $limit) $summary = substr($summary, 0, strrpos(substr($summary, 0, $limit), " ")) . "..."; echo $summary;
?>
</source>
Using several functions together to extract a portion of a string
<source lang="html4strict">
<?php
$test_string="testing testing Username:Michele Bond";
$position=strpos($test_string,"Username:");
$start=$position+strlen("Username:");
echo "$test_string
";
echo "$position
";
echo substr($test_string,$start);
?>
</source>
Using String Matching vs. Pattern Matching
<source lang="html4strict">
<?php $value = "my username"; if (strcmp($value, "user") == 0) {
echo "Found match in "" . $value . "" using strcmp.\n";
} else {
echo "Didn"t find match in "" . $value . "" using strcmp.\n";
} if (!(strrpos($value,"user") ==="false)) {
echo "Found match in "" . $value . "" using strrpos.\n";
} else {
echo "Didn"t find match in "" . $value . "" using strrpos.\n";
} if (ereg("\<user\>", $value)) {
echo "Found match in "" . $value . "" using ereg.\n";
} else {
echo "Didn"t find match in "" . $value . "" using ereg.\n";
} ?>
</source>