Example:
I want to make an url with different categorised variables. And there is an order. First variable is vegetables({variable01}), second is fruits({variable02}), third is trees ({variable03}) etc.
xxx.com/{variable01}-{variable02}-{variable03}-{variable04}-......
Yes I got this url.
BUT
What if a variable have two words(or three) AND I want the seperator is also a hyphen(brussels-sprouts)?
Example:
xxx.com/brussels-sprouts-{variable02}-{variable03}-{variable04}-......
or
xxx.com/{variable02}-green-apple-{variable03}-{variable04}-......
xxx.com/brussels-sprouts-green-apple
How can this be possible?
Thanks.
There is no way to specify the boundaries of your variables anymore, so it's only possible if you know the exact amount of variables and only 1 of your variables may contain hyphens. You can create a regex for that:
First variable:
^([\w-]+)-(\w+)-(\w+)-(\w+)$
Second variable:
^(\w+)-([\w-]+)-(\w+)-(\w+)$
But: if you know that each variable can have at most 1 hyphen, you can also do this:
/var1-foo1-var2-foo2-var3-foo3-var4
RewriteRule ^(\w+(-\w+)?)-(\w+(-\w+)?)-(\w+(-\w+)?)-(\w+(-\w+)?)$ index.php?var1=$1&var2=$3&var3=$5&var4=$7 [L]
Which results in:
array (
'var1' => 'var1-foo1',
'var2' => 'var2-foo2',
'var3' => 'var3-foo3',
'var4' => 'var4',
)
Related
I want to separate my array into separate strings that all end with a comma.
Array ( [0] => 233d3f9b-3e8e-4e16-ade2-6c165a0324c6
[2] => a6c736b0-f3d2-4907-9d36-6b31adeec2d1
[3] => 1e693cba-d0ce-4a24-bd75-b834e3f44272
)
The purpose of this is so i can pass it to an API. Like this
'optionUuids' => array(
$options2,
),
The option UUidds can contain multiple values as long its separated by a comma in the end.
I tried solving my problem using implode. Implode adds a comma at the end of each line, but it treats this comma as a string. I want to have multiple option UUids so i would need commas that are actually commas and not strings.
Im having trouble explaining this. This is what i expect:
'optionUuids' => array(
233d3f9b-3e8e-4e16-ade2-6c165a0324c6,
a6c736b0-f3d2-4907-9d36-6b31adeec2d1,
1e693cba-d0ce-4a24-bd75-b834e3f44272,
),
You should use
'optionUuids' => array_values(
$options2
),
This will give you all the values but with indices starting from zero
I think, what you want to achieve is not possible
You want to change something like this
["A","B","C"]
into something like this
"A","B","C"
With JS you could do things like this
// JavaScript
arr = [1,2,3]
opt = Array(...arr)
console.log(opt)
But this has no meaning in PHP (in fact it has a meaning, but this would not help you here.) What you would need is a sort of a spread operator like in JavaScript
Unfortunately the spread operator in PHP doesn't work this way. It knows the ... operator, but this is only used in function parameter declarations and function calls. #See https://secure.php.net/manual/en/functions.arguments.php#functions.variable-arg-list
I have an array $cat1 including
cat1[0]=>16 and cat1[1]=>16.
I also have this array:
$url_vars = array('text'=>$event->properties['text'],'SearchResultPagerPage'=>$thenextpage);
I need to put these combined into this URL function:
$this->URL('SearchResult','',$url_vars);
So that the resulting URL needs to look like this:
/SearchResult.html?text=cat&SearchResultPagerPage=1&cat1[]=1&cat1[]=16
Currently, if I combine them, I get this as the resulting combined array:
Array
(
[text] => cat
[SearchResultPagerPage] => 1
[0] => 1
[1] => 16
)
and this as the resulting URL:
SearchResult.html?&text=cat&SearchResultPagerPage=1&1=16
How do I form this so that it says cat1[]=1&cat1[]=16 instead of 1=16?
Thanks very much for any help anyone might offer!!
If you MUST use the URL function $this->URL('SearchResult','',$url_vars); then one idea is to use indices in the cat1 array. That is:
$url_vars["cat1[0]"] = 1;
$url_vars["cat1[1]"] = 16;
This will result in your query string having
...&cat1[0]=1&cat1[1]=16
with the []'s probably escaped, but perhaps your server script can properly accommodate these indices. It is worth a try. Otherwise you'll have to generate the URL outside of the URL function, because you can't have a php array with identical keys "cat1[]" but two separate values.
EDIT: One other thing to try in case your URL function is intelligent enough:
$url_vars["cat1"] = [1,16];
For a new application I'd like to work with the URI to determine what content should be loaded. Nothing special so far ... But how can I let the slug have slash(es) in it and make sure Zend Framework sees them as 1 variable? ZF splits the requested URL in chunks where every part is the string between 2 slashed. Now I'd like to have all the parts in 1 variable to work with.
For Example:
/de/my/page
de > language
my/page > 1 variable
Any ideas?
Advice from Hari K is the best choice, but if you really want to keep your slash, you can work with a Zend_Controller_Router_Route_Regex to catch your parameter, you just need to find a good regexp, a simple example :
$routetotry = new Zend_Controller_Router_Route_Regex('([^/]+)/(.*)',
array(1 => 'de', 'controller' => 'someController', 'action' => 'someAction'),
array(1 => 'lang',2=>'my_variable_with_slash'),
'%s/%s'
);
$router->addRoute('routetotry', $routetotry);
Custom router. Most flexible solution, but not the easiest one :(
Replace the slashes in the slug with someother value of your choice.
For example hyphen ( - ) or underscore ( _ ) .
I'm trying to change a variable name in a query string, so it's usable by my PHP code.
The query gets posts from an external system, so I can't control that they are posting a variable name with a space in it. And that makes it impossible for me to use the PHP $_GET function.
I need to change variable%20name to ?new1
And I need to change variable2 to new2
There are many variables passed in the query, but only these two need to be changed. The rest can stay the same or even disappear.
So ?variable%20name=abc&variable2=xyz
Needs to end up as ?new1=abc&new2=xyz
Also, they may not be in this order and there may be more variables
So ?variable%20name=abc&blah=123&blah2=456&variable2=xyz
Could end up as ?new1=abc&new2=xyz
OR as ?new1=abc&blah=123&blah2=456&new2=xyz
Either way would be fine!
Please give me the mod_rewrite rule that will fix this.
Thank you in advance!
Parsing the query string with mod_rewrite is a bit of a pain, has to be done with RewriteCond and using %n replacements in a subsequent RewriteRule, probably easier to manually break up the original query string in PHP.
The full query string can be found (within PHP) in $_SERVER['QUERY_STRING'].
You can split it up using preg_split() or explode(), first on &, then on =, to get key/value pairs.
Using custom%20cbid=123&blahblahblah&name=example as an example.
$params = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $cKeyValue) {
list ($cKey, $cValue) = explode('=', $cKeyValue, 2);
$params[urldecode($cKey)] = urldecode($cValue);
}
// Would result in:
$params = array('custom cbid' => 123,
'blahblahblah' => NULL,
'name' => example);
I'm passing through a variety of URLs in a global variable called target_passthrough, so the URL of a page might look like:
http://www.mysite.com/index.php?target_passthrough=example.com
Or something like that. Formats for that variable may be a variety of things such as (minus quotes):
"www.example.com"
".example.com"
"example.com"
"http://www.example.com"
".example.com/subdir/"
".example.com/subdir/page.php"
"example.com/subdir/page.php"
Please note how some of those have periods as the first character such as 2,5, and 6.
Now, what I am trying to do is pull out just "example.com" from any of those possible scenarios with PHP and store it to a variable to echo out later. I tried parse_url but it gives me the "www" when that is present, which I do not want. In instances where the url is just "example.com" it returns a null value.
I don't really know how to do regex matching or if that is even what I need so any guidance would be appreciated--not really that advanced at php.
As you pointed out, you can use parse_url to do much of the work for you and then simply strip off the www or leading dot if it is present.
An alternative strategy of taking the last two "words" won't always work because there are domains like www.example.co.uk. Using this strategy would give you co.uk instead of example.co.uk. There is no simple rule for determining which parts are the domain or the sub-domain.
parse_url() outputs an array the different parts of the URL. You are getting null values because you are only referencing the first item in the array. parse_url()
Array (
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)