I created a regex to match the domain name out of the input:
$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
$url = 'http://wwe.com';
if (preg_match($pattern, $url, $matches) === 1) {
echo $matches[0];
}
It works fine for this input:
http://google.com // output:google.com
But I am not able to achieve it for these inputs: (if the user enters an extra www
http://www.google.com // output:google.com
http://www.www.google.com // output:google.com
What am I missing?
Any help on this will be appreciated
What about this?
<?php
$urls = [
'http://google.com',
'http://www.google.com',
'http://www.www.google.com',
];
foreach($urls as $url) {
$url = parse_url($url, PHP_URL_HOST);
$url = preg_replace('/^(www\.)+/', '', $url);
echo $url . "\n";
}
Output:
google.com
google.com
www.google.com
Related
$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);
Using this script, I get the output:
http://website.com/632383970568166e82391f.png
And I need to extract the ID, which is abcdef and use it in another call. How can I do that?
Use regular expressions or do this:
$url = "http://subdomain.website.com/something/folder1/folder2/folder3/folder4/folder5/20385425_632383970568166e82391f.png";
$urlParts = explode('/', $url)
$imgFileParts = explode('_', $urlParts[(count($urlParts) - 1)]);
$imgId = $imgFileParts[0];
Simple regex
$re = "/\\S+\\/([\\S]+)_/";
$str = "http://subdomain.website.com/something/folder1/folder2/folder3/folder4/folder5/20385425_632383970568166e82391f.png";
preg_match($re, $str, $matches);
I am attempting to have a input field for adding a website/url. All I want to require to successfully submit the form is www.domainname.com; however, after submission I want to add back on http:// or https:// if it was not added by the person submitting the form.
The validation is along the lines of the following,
public function name($id) {
$input = Input::all();
$validator = Validator::make(
$input,
array(
'website' => 'active_url',
)
);
}
For this to work an if statement is needed. I have tried inserting something along the lines of the code listed below, but have not had any luck.
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
I am fairly new to PHP and just starting to use Laravel, so I apologize in advance if there is any confusion or lack of information. Any help is appreciated.
You could simply just strip the scheme to begin with and then add it.
$url = preg_replace('#^https?://#', '', $url);
$url = "http://" . $url;
Or
$url = ltrim('http://', $url);
$url = ltrim('https://', $url);
$url = 'http://' . $url;
Or
if (!preg_match('#^http(s)?://#', $url)) {
$url = 'http://' . $url;
}
This should work:
if (stripos($url, "http://") === false && stripos($url, "https://") === false) {
$url = "http://" . $url;
}
stripos is case-insensitive, so it shouldn't matter if the user typed in lowercase letters or not in the url prefix.
I think no need for validation,.. maybe it could help you
#alix axel code
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist
The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)
Example:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
After processing preg_replace, the outcome should be:
http://example.com/index.php?c=y&do=ms&r=k&p=7
I tried using:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).
You don't need RegEx for this:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
You could try using:
$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);
This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.
But if you want to use regexp:
$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;
This will remove all make in the URL
with rtrim you can remove last &
$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;
Just by using preg_replace
$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";
$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;
And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7
Hope this will be helpful to you guys.
I want to get Get Domain from URL and be output: http://www.domain.com/
I found this, but does not come out with the http://
<?php
$url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
$parse = parse_url($url);
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
print $parse['host']; // prints 'google.com'
?>
Output: www.lebanonpost.com
I want it to be: http://www.lebanonpost.com/
Try:
print $parse['scheme'] . '://' . $parse['host'];
It will work if there is https instead of http
Test Here
You can concate http:// to your output:
<?php
$url = 'http://www.lebanonpost.com/2012/05/20/press-754/';
$parse = parse_url($url);
$domain = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
$domainURL = $parse['scheme'].'://'.$parse['host'].'/';
print $domainURL;
?>
This is the resource I always use for printing url's with PHP - https://stackoverflow.com/a/8891890/1964113
This answer breaks down each piece, even http/https and #fragments.
Google these things man! Really easy to find.
PHP
<?php
$url = 'http://vimeo.com/25451551';
/* http://www.vimeo.com/25451551 ... www.vimeo.com/25451551 */
$url = preg_match('???', $url);
echo $url;
?>
Output
25451551
Any help with this would be appreciated. Thanks.
If video IDs are allowed to start with a 0, you may need to tune the following code a bit:
$url = 'http://vimeo.com/25451551';
sscanf(parse_url($url, PHP_URL_PATH), '/%d', $video_id);
// $video_id = int(25451551)
$url = 'http://vimeo.com/25451551/test';
$result = preg_match('/(\d+)/', $url, $matches);
if ($result) {
var_dump($matches[0]);
}