I want to remove some url in CI
if any echo like:
www.blabla.co/content/5DRwA/6Yt54/bla-bla
so, replace to:
www.blabla.co/content/bla-bla
nb: 6Yt54 and 5DRwA is a random value
How I remove URI Segment 2 from behind like that?
How can I solve it?
Try the uri class. First retrive uri with:
$array = $this->uri->segment_array();
Then remove second segment:
unset($array[2]);
Unfortunately your url doesnt seem to follow CI convention (you could use then $this->uri->assoc_to_uri($array)). So iterate over array to create uri.
$my_uri = ''; //better name it $my_uri not to conflict with $this->uri
foreach ($array as $value) {
$my_uri.= '/'.$value;
}
And then you can use your new uri f.e. to redirect:
redirect($uri);
Related
I've made a page /module/hello/?id_category=123, and I need to make the id_category rewritten to a name such as 123 would = abc.
Is there a way to achieve this without making 1000 manual redirects in the htaccess?
I know this can be done with a RewriteRule [L], but as far as I know you can't ask the database to convert the id to a name and then tell htaccess to rewrite the URL.
Thanks,
Luke
Use the below function and add id_category as string, stringToNumURL function returns you it's numeric mapping.
Then just include your URL content like this.
<?php
//http://mypage.com/module/hello/?id_category=abc
$str_arr = stringToNumURL($_GET['id_category']); //return 123
include_once("http://mypage.com/module/hello/".$str_arr);
function stringToNumURL($url){
$arr = array('a','b','c');
$arr_values = array_flip($arr);
$url = str_split($url);
$url_to_call="";
foreach ($url as $value) {
$url_to_call.=$arr_values[$value]+1;
}
return $url_to_call;
}
On the API I'm working on, a previous request generates a link that goes like this:
https://api.example.com/example/v1/individuals?$expand=emails%2Cphones&$skip=30
I need to get this "skip" param and send the data back to my backend, to process a request on the whole link plus the skip param, that changes along with the system.
Any tip on how to get this "skip" param?
remove $ sign from query param and try $value = request('skip') to get skip value
// Your api controller
public function someMethod(Request $request)
{
$request->query('skip', 10); // returns 30 for https://api.example.com/example/v1/individuals?expand=emails%2Cphones&skip=30 or 10 if skip is not set.
}
If you received the url from 3rd API and you don't wanna change it, you can do the following:
$urlFromApi = 'https://api.example.com/example/v1/individuals?$expand=emails%2Cphones&$skip=30';
//Remember to use single quote if you wanna paste string with '$' as a character with PHP.
$url = urlencode($urlFromApi);
$parts = parse_url($url);
parse_str($parts["path"], $a);
preg_match('/%24skip\%3D(\d+)/', $parts["path"], $matches);
$skip = $matches[1];
echo $skip;
I want to get foldername without any file name from a url in php?
My url is http://w3schools.com/php/demo/learningphp.php?lid=1348
I only want to retrieve the http://w3schools.com/php/demo from the url?
How to do this? Please help.
Try this,
$URL = 'http://w3schools.com/php/demo/learningphp.php?lid=1348';
$URL_SEGMENTS = explode("/", $URL);
foreach($URL_SEGMENTS as $Segment){
echo $Segment;
}
explode() will separate the string with / and provide an array. So you will have all url segments in foreach loop and you use it or store it in string or array.
After your comment let me show the script which will return your desire url.
$Desired_URL = $URL_SEGMENTS[2].'/'.$URL_SEGMENTS[3].'/'.$URL_SEGMENTS[4];
echo $Desired_URL;
If the url is stored in $url:
$url = 'http://w3schools.com/php/demo/learningphp.php?lid=1348';
You can do a preg_replace like so:
print(preg_replace('/\/[^\/]*$/', '', $url));
http://w3schools.com/php/demo
That regex means to replace everything from a / and all characters that are not / ... [^/]* ... to the end of the string ... $ ... with an empty string. Just delete them.
I was thinking if there is a way to hide part of the url in PHP/ Zend Framework 2. Something like this:
sitename.com/something/?inviter=1234&id=1
But I'd like to hide the part with the &id=1 somehow, so that when the url is copied and entered by the user, it would look like this:
sitename.com/something/?inviter=1234
And on the other side I can do something like this:
$id = $_GET["id"])
Is this possible to do, if so, how? Maybe there is something close to what I'm looking for to achieve?
You can hide it only with Cookie or Session techniques. But it will work only for one user during one session.
You can parse and rebuild the url using parse_url, http_build_url, with parse_str, and http_build_str.
For example:
/**
* Transform a url using a whitelist of query-string keys
*/
function transformUrlKeepQueryKeys($url, array $whitelist)
{
// Break the given url into parts
$parts = parse_url($url);
// Break the parts into key-value pairs
$query = $parts['query'];
parse_str($query, $queryParts);
// Unset all unwanted keys
foreach (array_keys($queryParts) as $k) {
if (!in_array($k, $whitelist)) {
unset($queryParts[$k]);
}
}
// rebuild the url
$parts['query'] = http_build_query($queryParts);
// return
return http_build_url('', $parts);
}
Invocation should be:
$url = 'http://sitename.com/something/?inviter=1234&id=1';
$whitelist = [
'inviter'
];
$expectedUrl = 'http://sitename.com/something/?inviter=1234';
$actualUrl = transformUrlKeepQueryKeys($url, $whitelist);
assert($expectedUrl == $actualUrl);
Alternatively, you could implement something similar using a blacklist of keys to remove.
The only problem with this is that the function http_build_url is not included in core PHP, but is part of the PECL HTTP extension. If you are unable to install that extension in your environment, then you can use a pure PHP implementation of that function, for example here.
I want to pass a url like http://example.com/test?a=1&b=2 in url segment of codeigniter.
I'm trying to pass something like this http://myurl.com/abc/http://example.com/test?a=1&b=2 and get the "http://example.com/test?a=1&b=2" url. What should be the best way to do this?
Set your URI protocol to REQUEST_URI in application/config/config.php , like this:
$config['uri_protocol'] = 'REQUEST_URI';
then use GET method:
$this->input->get('a');
EDIT:
Since http://example.com/test?a=1&b=2 is not encoded URL, it isn't possible. So first, I would encode URL with urlencode function like this:
urlencode('http://example.com/test?a=1&b=2');
it returns something like: http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2
So I would pass the URL like this:
http://myurl.com/?url=http%3A%2F%2Fexample.com%2Ftest%3Fa%3D1%26b%3D2
then get an example URL with GET method.
$this->input->get('url');
Use this technique to get the URL
$url = "http://example.com/test?a=1&b=2";
$segments = array($controller, $action, $url);
echo site_url($segments);
// or create a anchor link
echo anchor($segments, "click me");
Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
<?php
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF