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
<?
$string = "This is a strpos( ) test";
$pos = strpos($string, "i", 3);
if ($pos = == false) {
print "Not found\n";
} else {
print "Found at $pos!\n";
}
?>
Finding a substring with strpos()
<?php
if (strpos($_POST["email"], "@") === false) {
print "There was no @ in the e-mail address!";
}
?>
Finding the Position of a Substring with strpos()
<?php
$membership = "asdfasdf";
if ( strpos($membership, "mz") === 0 ) {
print "hello mz";
}
?>
int strpos ( string haystack, mixed needle [, int offset] ) returns the index of the beginning of a substring"s first occurrence within a string
<?
$string = "This is a strpos( ) test";
print strpos($string, "s") . "\n";
?>
Return String found at position 22
<?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";
?>
strpos
<?php
$substr = "index.html";
$log = "192.168.1.11:/www/htdocs/index.html:";
$pos = strpos($log, $substr);
echo "$pos";
?>
strpos() function finds the position of the first occurrence in string.
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;
?>
strpos( ) will return false (as opposed to -1)
<?
$string = "This is a strpos( ) test";
$pos = strpos($string, "This");
if ($pos = == false) {
print "Not found\n";
} else {
print "Found!\n";
}
?>
strrpos() function locates the last occurrence of character in string.
Its syntax is: int strrpos (string string, char character)
<?
print strrpos("asdf","d");
?>
strrpos.php
<?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;
?>
Using several functions together to extract a portion of a string
<?php
$test_string="testing testing Username:Michele Bond";
$position=strpos($test_string,"Username:");
$start=$position+strlen("Username:");
echo "$test_string<br />";
echo "$position<br />";
echo substr($test_string,$start);
?>
Using String Matching vs. Pattern Matching
<?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";
}
?>