So i worked in a project that i need to encode my URL, for example in my URL i have :
$url = 'http://www.test.com/?sms=+212&text=bla bla'
urlencode($url);
$url = urlencode($url);
Also i tried rawurlencode($url); and i got the same Url without changes !!
so please if someone has any idea i will be so appreciative :)
$url = "http://www.test.com/?sms=+212&text=bla bla";
echo $url = urlencode($url);
urlencode returns a new encoded url, doesn't change the $url passing to it. try this
$encoded_url = urlencode($url);
$encoded_url is what you want
Ur url data should be string.
<?php
$url = 'http://www.test.com/?sms=+212&text=bla bla';
$encoded = urlencode($url);
echo(urlencode($url)); //simple urlencode
echo('<br>');
echo($encoded);//output with varible
echo('<br>');
$url = urlencode($url);
echo($url);
Be attentive in what place u redefined ur url var. Output of that code looks like:
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
The second one really works:
$url = "http://www.test.com/?sms=+212&text=bla bla";
$url = urlencode($url);
echo $url;
See a demonstration.
I also noticed that
$url = http://www.test.com/?sms=+212&text=bla bla
is an invalid string format. Use ' ' or " " for strings:
$url = "http://www.test.com/?sms=+212&text=bla bla";
Related
I have string url variable;
$url ="http://carkva-gazeta.org/римско-католическая-церковь/";
I need transform $url to:
"http://carkva-gazeta.org/%d1%80%d0%b8%d0%bc%d1%81%d0%ba%d0%be-%d0%ba%d0%b0%d1%82%d0%be%d0%bb%d0%b8%d1%87%d0%b5%d1%81%d0%ba%d0%b0%d1%8f-%d1%86%d0%b5%d1%80%d0%ba%d0%be%d0%b2%d1%8c/"
I have tried: rawurlencode($url);
and urlencode($url);
But result is:
http%3A%2F%2Fcarkva-gazeta.org%2F%D1%80%D0%B8%D0%BC%D1%81%D0%BA%D0%BE-%D0%BA%D0%B0%D1%82%D0%BE%D0%BB%D0%B8%D1%87%D0%B5%D1%81%D0%BA%D0%B0%D1%8F-%D1%86%D0%B5%D1%80%D0%BA%D0%BE%D0%B2%D1%8C%2F
$url = "http://carkva-gazeta.org/";
$url .= urlencode("римско-католическая-церковь");
echo $url;
Like so?
Probably that's the best solution:
$url ="http://carkva-gazeta.org/римско-католическая-церковь/";
$x = parse_url($url);
echo $x['scheme'].'://'.$x['host'].strtolower(str_replace('%2F','/',urlencode($x['path'])));
I've used also strtolower to make it lowercase as you wanted
Assuming that you are getting your URL string automatically/dynamically and that it is not a fixed string that you can simply split while writing your code, you'll want something like this
$url = "http://carkva-gazeta.org/римско-католическая-церковь/";
// in case it is https, we don't want to hardcode http
$scheme = parse_url($url, PHP_URL_SCHEME);
$host = parse_url($url, PHP_URL_HOST);
// do not encode the first '/' or the last '/'
$encodedPath = strtolower(urlencode(substr(parse_url($url, PHP_URL_PATH), 1, -1)));
$encodedUrl = $scheme . "://" . $host . "/" . $encodedPath . "/";
DEMO
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 have the below function that works perfect when I put the URL string within the argument manually. I need it to be dynamic though and I am using Wordpress.
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
// Below is the one that works manually
<?php echo get_tweets('http://www.someurl.com');
//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();
echo get_tweets('$url');
echo get_tweets($url);
$url = '$get_permalink()';
$url = $get_permalink(); // produces needs to be in string error
echo get_tweets($url);
There is nothing wrong with what you're doing, per se. The only obvious mistake I can see is that you aren't encoding the URL properly. You need to ensure the query string arguments you put in the URL are properly URL encoded, otherwise the remote host may not interpret the request correctly.
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode($url));
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
echo get_tweets('http://www.someurl.com'); // should work just fine
Did you try to urlencode your url String?
urlencode($foo);
Your main problem is on below line
Change
//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();
To
//ones I have tried that do not (trying to make dynamic)
$url = get_permalink();
Im totally new to regex. I need your help.
What is the regex function for changing the below url to url listed downside?
How to use that regex function in PHP.
https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg
TO
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320/393656_257350694313804_126044397444435_712409_344887174_n.jpg
OR
https://fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480/393656_257350694313804_126044397444435_712409_344887174_n.jpg
Thanks in advance.
$size = "s320x320";
$url = preg_replace("#https://(.*)/(.*)/(.*)\_s.jpg#i", "https://$1/$2/$size/$3_n.jpg", $url);
$url = str_replace("-photos-", "-sphotos-", $url);
This code is untested, but should work. The third line, str_replace, is used to make the regex simpler. :)
preg_replace( '#^(.*?)photos(.*)(/[^/]*?)_s.jpg$#', '$1sphotos$2/s320x320$3_n.jpg', $url );
Ok, let me try to answer with some code
$url = 'https://fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4/393656_257350694313804_126044397444435_712409_344887174_s.jpg';
$str = array('fbcdn-photos-a.akamaihd.net/hphotos-ak-ash4', '_s');
$rep1 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s320x320', '_n');
$rep2 = array('fbcdn-sphotos-a.akamaihd.net/hphotos-ak-ash4/s480x480', '_n');
//TO
$url1 = str_replace($str, $rep1, $url);
//OR
$url2 = str_replace($str, $rep2, $url);
On a page of mine, I have a GET as a URL of a website.
mypage.com/page.php?=URLHERE
On this URL, I need the ID at the very end of the URL
mypage.com/page.php?url=http://www.otherwebsite.com/something.php?id=%%%%%%%
These numbers are sometimes different amount of digits, so how would I do that?
My code:
$url = $_GET['url'];
Assuming the url parameter is a properly encoded URL, then useparse_url() to get the URL components and parse_str() to retrieve the id parameter from its query string.
$url = $_GET['url'];
// First parse_url() breaks the original URL up
$parts = parse_url($url);
// parse_str() parses the query string from the embedded URL
$query = parse_str($parts['query']);
// Finally, you have your full id parameter
$id = $query['id'];
assuming the the url has id at the begining of query_string
<?php
$url = $_GET['url'];
$url = basename($url);
$url =explode("?",$url);
$url = explode("=",$url[1]);
echo $url[1];
?>
try the parse_url
$url = parse_url($_GET['url'], PHP_URL_QUERY);
$query = explode('=', $url);
$id = $query[1];
I'd use the PHP function explode() http://php.net/manual/en/function.explode.php
For this example
$numbers = explode("=",$url);
$id_value = $numbers[2];
print $id_value;