I am using file_get_content($url) which does not work with url starting from www.
So I am trying to append the http:// and converting into proper form if user entered url is not in correct form.
Check DEMO HERE
<?php
$url= 'www.google.com';
$pad = 'http://';
$cmp = 'www';
$prefix = substr($url , 0,2);
if($cmp == $prefix)
{
echo str_pad($url, strlen($url)+3 ,"$pad",STR_PAD_LEFT);
}
?>
This code does not echo correct url. Any issue here?
Why not use parse_url to figure it out?
$url = "www.example.com/test.php";
$parsedUrl = parse_url($url);
if(!array_key_exists('scheme', $parsedUrl)){
$url = "http://" . $url;
}
echo $url;
codepad example.
This is all you need:
if (strpos($url, '://') === false)
$url = 'http://' . $url;
check this
$url= 'www.google.com';
$pad = 'http://';
$cmp = 'www';
$prefix = substr($url , 0,3);
if($cmp == $prefix)
{
echo str_pad($url, strlen($url)+7 ,"$pad",STR_PAD_LEFT);
}
Related
I want to convert a URL to regular expression to match it with current URL. For example, I have a URL http://www.example.com/example.php
I want it to convert to
^(https?://)?(www\.)?example\.com\/example\.php/?(\?.)?(#.)?$
So that I store it and whenever a user hits this url with any number of parameters attached to it, I will match that url with my regular expression and will perform some action based on the results.
I have found many questions but they all are either to match general URL (with any domain name) or with regular expressions given. But I want a function to which I will pass URL and it will return its regular expression and I will use it to match that specific domain.
I have finally created this code with the help of stackoverflow and other communities. This provides me the exact string I require against given URL.
<?php
function createrRegex($url) {
$var1 = '^(https?://)?';
$host = parse_url($url, PHP_URL_HOST);
$host_parts = explode('.', $host);
if (!empty($host_parts)) {
$length = count($host_parts);
foreach ($host_parts as $i => $part) {
if ($i == 0) {
if ($part == "www") {
$var1 .= '(' . $part . '\\\\.)?';
} else {
$var1 .= '' . $part;
$var1 .= ($i < ($length - 1)) ? '\\\\.' : '';
}
} else {
$var1 .= '' . $part;
$var1 .= ($i < ($length - 1)) ? '\\\\.' : '';
}
}
}
$path = '';
if ((parse_url($url, PHP_URL_PATH) != NULL)) {
$path = str_replace('/', '\\\\/', parse_url($url, PHP_URL_PATH));
$path = str_replace('.', '\\\\.', $path);
}
$var1 .= $path;
$var1 .= '/?(\\\\?.*)?(#.*)?$';
return $var1;
}
?>
I'm attempting to retrieve the last part of a URL before the trailing backslash. I have used this previously, which worked great, but the URL on the site I was developing back then did not have a trailing slash. Below is the code I used for that.
$link = $_SERVER["REQUEST_URI"];
$link_array = explode('/',$link);
echo $page = end($link_array);
Any help would be appreciated,
Kind Regards,
Rees
This works for me
$link = $_SERVER["REQUEST_URI"];
if(substr($link, -1) == '/') {
$link = substr($link, 0, -1);
}
$link_array = explode('/',$link);
echo $page = strtoupper(end($link_array));
you could try :
$link = $_SERVER["REQUEST_URI"];
$link_array = explode('/',$link);
$lastPart = str_replace('/', '', $link_array[count($link_array) - 1]);
You are almost there. You have to pick the second last value:
$link = $_SERVER["REQUEST_URI"];
$link_array = explode('/',$link);
end($link_array);//move cursor to the end
//pick last or second last depending on trailing slash
$page = substr($link,-1) == "/" ? prev($link_array) : current($link_array);
echo $page;
You can use php's parse_url to parse the url and get the wanted components.
or
EDIT:
$url = 'http://' . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
if (substr("url", -1) == '/') {
rtrim($url , "/")
}
$lastPart = substr($url, strrpos($url, '/') + 1);
This is from Stackoverflow posts:
Get the full URL in PHP
Get characters after last / in url
how to convert (in php)
http://images.example.com//images/2014/12/14-12-27-114545کریم باقری در
تمرین تراکتور3 (300x200).jpg
to
http://images.example.com//images/2014/12/14-12-27-114545%DA%A9%D8%B1%DB%8C%D9%85%20%D8%A8%D8%A7%D9%82%D8%B1%DB%8C%20%D8%AF%D8%B1%20%D8%AA%D9%85%D8%B1%DB%8C%D9%86%20%D8%AA%D8%B1%D8%A7%DA%A9%D8%AA%D9%88%D8%B13%20(300x200).jpg
Note: There are space in first URL.
I try this code but I failed:
function url_path_encode($url) {
$path = parse_url($url, PHP_URL_PATH);
if (strpos($path,'%') !== false) return $url; //avoid double encoding
else {
$encoded_path = array_map('urlencode', explode('/', $path));
return str_replace($path, implode('/', $encoded_path), $url);
}
http://images.example.com//images/2014/12/14-12-27-114545%DA%A9%D8%B1%DB%8C%D9%85+%D8%A8%D8%A7%D9%82%D8%B1%DB%8C+%D8%AF%D8%B1+%D8%AA%D9%85%D8%B1%DB%8C%D9%86+%D8%AA%D8%B1%D8%A7%DA%A9%D8%AA%D9%88%D8%B13+%28300x200%29.jpg
online tools example: http://meyerweb.com/eric/tools/dencoder/
You try this?
(PHP 4, PHP 5)
urlencode — URL-encodes string
<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>
You look for rawurlencode
function url_path_encode($url)
{
$ret = '';
if(false === $parts = parse_url($url)) {
return false;
}
if (isset($parts['scheme'])) {
$parts['scheme'] = $parts['scheme'] . '://';
}
if (isset($parts['path'])) {
$parts['path'] = join('/', array_map('rawurlencode', explode('/', $parts['path'])));;
}
// ...
return implode('', $parts);
}
echo url_path_encode('http://images.example.com//images/2014/12/14-12-27-114545کریم باقری در تمرین تراکتور3 (300x200).jpg');
Finally I solved it via this functions:
function url_path_encode($url) {
$unescaped = array(
'%2D'=>'-','%5F'=>'_','%2E'=>'.','%21'=>'!', '%7E'=>'~',
'%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')'
);
$reserved = array(
'%3B'=>';','%2C'=>',','%2F'=>'/','%3F'=>'?','%3A'=>':',
'%40'=>'#','%26'=>'&','%3D'=>'=','%2B'=>'+','%24'=>'$'
);
$score = array(
'%23'=>'#'
);
return strtr(rawurlencode($url), array_merge($reserved,$unescaped,$score));
}
Say if I have two strings
$first = 'http://www.example.com';
$second = 'www.example.com/';
How could I determine they match? I just care that the example part matches. I'm thinking some form of Regex pattern would match but I can't figure it out at all.
Don't use a regex if you're trying to evaluate structured data. Regexes are not a magic wand you wave at every problem that happens to involve strings. What if you have a URL like http://www.some-other-domain.com/blah/blah/?www.example.com?
If you're trying to match a domain name to a domain name, then break apart the URL to get the host and compare that. In PHP, use the parse_url function. That will give you www.example.com as the host name, and then you can compare that to make sure it is the hostname you expect.
Try this
function DomainUrl($x) {
$url = $x;
if ( substr($url, 0, 7) == 'http://') { $url = substr($url, 7); }
if ( substr($url, 0, 8) == 'https://') { $url = substr($url, 8); }
if ( substr($url, 0, 4) == 'www.') { $url = substr($url, 4); }
if ( substr($url, 0, 4) == 'www9.') { $url = substr($url, 4); }
if ( strpos($url, '/') !== false) {
$ex = explode('/', $url);
$url = $ex['0'];
}
return $url;
}
$first = DomainUrl('http://www.example.com');
$second = DomainUrl('www.example.com/');
if($first == $second){
echo 'Match';
}else{
echo 'Not Match';
}
How to get first 3 parts of current URL by using PHP.
For example:
My Url: http://something.com/somebody/somegirls/whatever/
The result after getting parts: http://something.com/somebody/somegirls/
This is my code PHP which get current URL:
<?php function curPageURL() {
$url = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
$url .= '://' . $_SERVER['SERVER_NAME'];
$url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
$url .= $_SERVER['REQUEST_URI'];
return $url;
}
$current_url = str_replace("www.", "", curPageURL());
?>
Try this,
<?php
$url = 'http://something.com/somebody/somegirls/whatever/';
$parts = explode('/', $url);
$new_url = $parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4].'/';
echo $new_url;
?>
OUTPUT
http://something.com/somebody/somegirls/
Assuming that you have grabbed this URL from your function...
<?php
$url='http://www.something.com/somebody/somegirls/whatever/';
$parts=explode('/',parse_url($url)['path']);
array_unshift($parts,trim(strstr(parse_url($url)['host'],'.'),'.'));
print_r(array_filter($parts));
OUTPUT :
Array
(
[0] => something.com
[2] => somebody
[3] => somegirls
[4] => whatever
)
Demonstration
You can also use parse_url to get the url in parts in an array like this:
$current_url_array = parse_url($current_url);
var_dump($current_url_array);
You can use regexp:
<?php
function getFirstUrlContents($Url) {
preg_match_all('/^([^\/]*\/){5}/', $Url, $MatchesArray);
return $MatchesArray[0];
}
var_dump(getFirstUrlContents('http://something.com/somebody/somegirls/whatever/'));
?>
<?php
$url='http://something.com/somebody/somegirls/whatever' ;
$explode=explode("/", $url);
$search=end($explode);
echo $currentUrl=str_replace($search,'',$url);
?>
Output
http://something.com/somebody/somegirls/
Try with this :
<?php
$url = 'http://something.com/somebody/somegirls/whatever/';
$pos = explode('/', $url);
for($i=0; $i<5; $i++){
echo $pos[$i].'/';
}
?>
Output: http://something.com/somebody/somegirls/
Please check the below code.
function createUrl($array, $pos) {
$string = '';
for ($i = 0; $i < $pos; $i++)
$string .=$array[$i].'/';
return $string;
}
$current_url = "http://something.com/somebody/somegirls/xyz/yui";
$initial_string = (stripos($current_url, 'https://') !== FALSE)
? 'https://'
: ((strpos($a, 'http://') !== FALSE)
? 'http://' : '');
$last_string = explode('/', substr($a, strlen($initial_string)));
$final_url = $initial_string.
(count($last_string) > 3)
? createUrl($last_string, 3)
: substr($current_url, strlen($initial_string));
echo $final_url;