PHP_URL_HOST
, а дальше сравниваем на эквивалентность.<?php
function extract_host($url)
{
if (preg_match('~^https?://(?:www\.)?([^/]+)~', $url, $m)) {
return $m[1];
}
return null;
}
var_dump(extract_host('http://example.com/go/d5863567e7544ae94c5c9342837dbbf8'));
var_dump(extract_host('http://www.example.com/go/d5863567e7544ae94c5c9342837dbbf8'));
string(11) "example.com"
string(11) "example.com"
/**
* Check if a given ip is in a network
* @param string $ip IP to check in IPV4 format eg. 127.0.0.1
* @param string $range IP/CIDR netmask eg. 127.0.0.0/24, also 127.0.0.1 is accepted and /32 assumed
* @return boolean true if the ip is in this range / false if not.
*/
function ip_in_range( $ip, $range ) {
if ( strpos( $range, '/' ) == false ) {
$range .= '/32';
}
// $range is in IP/CIDR format eg 127.0.0.1/24
list( $range, $netmask ) = explode( '/', $range, 2 );
$range_decimal = ip2long( $range );
$ip_decimal = ip2long( $ip );
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}
<?php
$html = <<<EOF
<img height="720" src="http://www.example.com/static/ckef/img/2_122.jpg" width="960">
<img height="720" src="http://www.example.com/static/ckef/img/2_122.jpg" width="960">
<img height="720" src="http://www.example.com/static/ckef/img/2_122.jpg" width="960">
EOF;
$dom = new DOMDocument();
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
$result = '';
/** @var DOMElement $image */
foreach ($images as $image) {
$image->removeAttribute('width');
$image->removeAttribute('height');
$result .= $dom->saveHTML($image);
}
echo $result, PHP_EOL;
<img src="http://www.example.com/static/ckef/img/2_122.jpg"><img src="http://www.example.com/static/ckef/img/2_122.jpg"><img src="http://www.example.com/static/ckef/img/2_122.jpg">
php > preg_match('/wall-(\d+)_(\d+)/', 'https://vk.com/wall-1234567_890', $matches);
php > var_dump($matches);
array(3) {
[0]=>
string(16) "wall-1234567_890"
[1]=>
string(7) "1234567"
[2]=>
string(3) "890"
}
list(, $post_id, $owner_id) = $matches;
<?php
preg_match('/wall-(\d+)_(\d+)/', 'wall-1234567_890', $matches);
list(, $post_id, $owner_id) = $matches;
var_dump($post_id, $owner_id);
const middleware = store => next => action => {
// before-action logic is above
next(action);
// after-action logic is below
};
public function handle($request, Closure $next)
{
// before-action logic is above
$result = $next($request);
// after-action logic is below
}