preg_
"/regex/"
preg_match(regex,string,[captures])
- Return if the regex is found in the string, optionally capturing groups into provided arraypreg_match_all(regex,string,[captures])
- Finds all matches, storing them in the provided capture variable, and returns the number of matches foundpreg_grep(regex,array)
- Returns an array with all elements of the input array that match the regular expression$str = <<< END
The phone number for the CSEE Department is 410-455-3500.
The fax number is 410-455-3969.
END;
preg_match("/\d{3}-\d{3}-\d{4}/",$str);
preg_match("/(\d{3})-(\d{3})-(\d{4})/",$str,$matches);
echo $matches[0];
echo $matches[1];
echo $matches[2];
echo $matches[3];
preg_match_all("/(\d{3})-(\d{3})-(\d{4})/",$str);
$n_matches = preg_match_all("/(\d{3})-(\d{3})-(\d{4})/",$str,
$matches);
for($i = 0; $i < $n_matches; $i++){
echo $matches[0][$i];
echo $matches[1][$i];
echo $matches[2][$i];
echo $matches[3][$i];
}
$strs = array('One','Bone','Phone','Tome','Rome');
preg_grep('/one/i',$strs)
$strs = array('a'=>'One','b'=>'Bone',
'c' => 'Phone','d' => 'Tome','e' => 'Rome');
preg_grep('/one/i',$strs)
$strs = array('One','Bone','Phone','Tome','Rome');
preg_grep('/one/i',$strs,PREG_GREP_INVERT)
$text = <<< END
<footer role="contentinfo" id="umbc-footer">
<div id="umbc-footer-logo" class="first-child">[<span class="first-child last-child">UMBC:
An Honors University in Maryland</span>](https://www.umbc.edu "UMBC: An Honors University in Maryland")</div>
<div id="usmd-footer-logo">[<span class="first-child last-child">Univeristy
System of Maryland</span>](http://www.usmd.edu/ "Univeristy System of Maryland")</div>
<nav id="umbc-footer-nav" style="clear:both;">
* [About UMBC](https://about.umbc.edu)
* [Contact Us](mailto:homepage@umbc.edu)
* [Equal Opportunity](https://www.umbc.edu/go/equal-opportunity)
* Community: [<span class="none first-child last-child">Facebook</span>](https://www.facebook.com/umbcpage)
[<span class="none first-child last-child">Twitter</span>](http://twitter.com/umbc)
[<span class="none first-child last-child">YouTube</span>](http://www.youtube.com/user/UMBCtube)
[<span class="none first-child last-child">Retriever Net</span>](http://alumni.umbc.edu/)
</nav>
<div id="umbc-footer-info" class="last-child">© University of Maryland, Baltimor
e County <span class="bullet first-child">•</span> 1000 Hilltop C
ircle <span class="bullet last-child">•</span> Baltimore, MD 21250</div>
</footer>
END;
preg_replace(regex,replacement,string)
$str = <<< END
Go directly to Jail.
Do not pass Go,
do not collect $200.
END;
$str = preg_replace('/(do not)/i', '<b>$1</b>', $str);
echo $str;
$chipmunks = array("Alvin","Simon","Theodore");
$chipmunks = preg_replace('/^(.*)$/',"$1 Seville",$chipmunks);
foreach($chipmunks as $chip){
echo $chip;
}
$bad_words = array("/Fooey/i", "/Blurgh/i",
"/Smurf/i", "/Flipping/i");
$str = <<<HERE
Oh fooey, stop saying smurf, we are trying to
air this on flipping network tv.
HERE;
preg_replace($bad_words,"*******", $str);
$bad_words = array("/Fooey/i", "/Blurgh/i",
"/Smurf/i", "/Flipping/i");
$replacements = array("F***","B****","S***","F*******");
$str = <<<HERE
Oh fooey, stop saying smurf, we are trying to air this on flipping network tv.
HERE;
preg_replace($bad_words,$replacements, $str);
$lines = file('sherlock.txt');
preg_replace('/Sherlock Holmes/','Bond, James Bond',$lines);
$lines = file('sherlock.txt');
$names = array('/Sherlock Holmes/','/Henry Baker/',
'/Mr[s]\. \w+/');
preg_replace($names,'REDACTED',$lines);
$ssns = <<<END
078-05-1120 is no longer issued because so many people used it as their
own. Other fake social security numbers include 000-11-1111, and 123-00-1111.
Pretty much any number with all zeros in one location isn't a real number, such
as 1234-56-0000
END;
preg_quote(string)
function will place a backslash before any special character$string_to_search_for = "The cost of the vase was $100!!";
preg_quote($string_to_search_for);
*
is all matches, don't need .*
glob
function returns a list of full file namesglob("/home/bryan/Teaching/CMSC433/*.html")
sort
rsort
$number_array = array(6,7,8,4,2,6,7,5,4,3);
sort($number_array);
$number_array;
$number_array = array(6,7,8,4,2,6,7,5,4,3);
rsort($number_array);
$number_array;
$string_array = array("asdf","rteu","qerw","vbnm");
sort($string_array);
$string_array;
$string_array = array("asdf","rteu","qerw","vbnm");
rsort($string_array);
$string_array;
$assoc_array = array('key1' => 'tiger',
'key2' => 'lion',
'k3' => 'cheeta');
sort($assoc_array);
$assoc_array;
$mixed_array = array(5,6,4,3,1,"asdf","rteu",4,"qerw","vbnm","1");
sort($mixed_array);
$mixed_array;
$mixed_array = array(5,6,4,3,1,"asdf","rteu",4,"qerw",
"vbnm","1");
sort($mixed_array,SORT_STRING);
$mixed_array;
a*sort()
functionsasort()
arsort()
$assoc_array = array('key1' => 'tiger',
'key2' => 'lion',
'k3' => 'cheeta');
asort($assoc_array);
$assoc_array;
$assoc_array = array('key1' => 'tiger', 'key2' => 'lion',
'k3' => 'cheeta');
arsort($assoc_array);
$assoc_array;
$number_array = array(6,7,8,4,2,6,7,5,4,3);
asort($number_array);
$number_array;
k*sort()
functionsksort()
krsort()
aksort()
functionk*sort()
mantains associativity$cats = array('asia' => 'tiger',
'africa' => 'lion',
'south_america' => 'jaguar',
'north_america'=>'bobcat');
ksort($cats);
$cats;
$cats = array('asia' => 'tiger',
'africa' => 'lion',
'south_america' => 'jaguar',
'north_america'=>'bobcat');
krsort($cats);
$cats;
$number_array = array(6,7,8,4,2,6,7,5,4,3);
ksort($number_array);
$number_array;
To use a custom sort function, call one of the u*sort
functions
usort()
uasort()
uksort()
Each of these functions takes an array and a comparator function
$semesters = array('Fall 2009','Spring 2010',
'Winter 2010','Summer 2010',
'Fall 2010');
shuffle($semesters);
$semesters;
usort($semesters, function($a, $b) {
$map = array('Winter'=>1, 'Spring'=>2,
'Summer'=>3, 'Fall'=>4);
$aParts = explode(' ', $a);
$bParts = explode(' ', $b);
$result = $aParts[1] - $bParts[1];
return $result == 0 ? $map[$aParts[0]] - $map[$bParts[0]] : $result;
});
$semesters
$cats = array('asia' => 'tiger',
'africa' => 'lion',
'south_america' => 'jaguar',
'north_america'=>'bobcat');
uasort($cats,function($a,$b){
return strcmp(substr($a,-1),substr($b,-1));
});
$cats;
$cats = array('asia' => 'tiger',
'africa' => 'lion',
'south_america' => 'jaguar',
'north_america'=>'bobcat');
uksort($cats,function($a,$b){
return strlen($b) - strlen($a);
});
$cats;
$movies = array('Avengers' => array('box'=> 250000000, 'rating' => 'PG-13', 'days' => 3),
'I Feel Pretty' => array('box' => 29000000 , 'rating' => 'PG-13', 'days' => 10),
'Sherlock Gnomes' => array('box' => 40000000, 'rating' => 'PG' , 'days' => 38),
'Peter Rabbit' => array('box' => 114000000, 'rating' => 'PG', 'days'=>80 ),
'A Quiet Place' => array('box' => 148173301, 'rating' => 'PG-13', 'days'=>24 ),
'Black Panther' => array('box' => 688000000, 'rating' => 'PG-13', 'days'=>73 )
);
http://
ftp://
file://
$website = file_get_contents("http://umbc.edu");
$website;
preg_match_all("/<a href=\"(.*?)\">(.*?)<\/a>/",$website,
$matches);
$matches[1];
$website_fp = fopen('http://umbc.edu','r');
while($line = fgets($website_fp)){
echo htmlspecialchars($line);
}
curl_init()
curl_setopt()
curl_exec()
curl_close()
curl_init
can be called in one of two waysCURLOPT_
curl_setopt(curl_object, OPTION_CONSTANT, value);
$ch = curl_init('http://umbc.edu');
$result = curl_exec($ch);
curl_close($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://umbc.edu");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$result;
$lat = 39.2556015207;
$long = -76.71099543;
$radius= 700;
if($radius > 10000){
$radius = 10000;
}
$curl = curl_init("https://en.wikipedia.org/w/api.php?action=query&prop=coordinates|pageimages|pageterms|info|extracts&colimit=100&piprop=thumbnail&pithumbsize=144&pilimit=50&wbptterms=description&generator=geosearch&ggscoord=${lat}|${long}&ggsradius=${radius}&ggslimit=100&ggsprop=type&format=json&inprop=url");
curl_setopt($curl, CURLOPT_USERAGENT, 'UMBC_CMSC_433_Project/1.0 (https://www.csee.umbc.edu/~bwilk1/433/; bwilk1@umbc.edu)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
echo print_r(json_decode($data),true);
curl_close($curl);
curl_getinfo
function can be passed an curl handle, and optionally flags to denote what info to get$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://umbc.edu");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$r1 = curl_exec($ch);
$result = curl_getinfo($ch);
curl_close($ch);
$result;