I used regexpal.com to test my regexp against the data Wordpress is trying to compare to and it fails, look at this and tell me if you see the problem?
The regexp
"#^json/(.+?)/?([a-zA-Z0-9]*)?$#"
The content to match
json/trips
These works, the previous one doesn't
json/trips/0
json/trips/13
json/fullticket/9805048001130122361809
If I try all these in regexpal they all work, but in wordpress, only the one that doesn't contain the id of the element I want to fetch fails the others work fine.
Interrestingly enough, the $matches return this:
array
0 => string 'json/trips' (length=10)
1 => string 't' (length=1)
2 => string 'rips' (length=4)
Try this regexp instead :
#^json/([^/]+)/?([a-zA-Z0-9]*)?$#
Output :
Array
(
[0] => json/trips
[1] => trips
[2] =>
)
The answer after tweaking the wordpress rewrite rule a bit more ends up being:
data/([^/]+)(/([a-zA-Z0-9\-]*))?$
Note: i changed json to data in the new scenario so i don't mess up the custom post type rules
Related
I've trying to make my own route class - and I've hot problem with parsing valid url's.
This is an example url:
http://localhost/app/module/command/i15
And I need every element after slash / to be an additional element in array creating using preg_match in php.
So - for searching of numbers I've got: ([0-9]+)
Searching strings that contains letters, signs like - or _ I've got ([^.]+) (here's the problem)
whole regexp looks like this:
(app)\/(module)\/([^.]+)\/i([0-9]+)
i want an array of 5 elements, something like that:
0 => app, 1=> module, 2 => command, 3 => i15
It will be more beauty if preg_match will return only something like that:
0 => app, 1=> module, 2 => command, 3 => 15 (without I as param identifier)
Can anyone quick help me with this ? I stuck 2 days in that, and can't find right solution.
EDIT:
I've got some regexp predefined like:
/app/contacts/list => (app)\/(contacts)\/([^.]+)
/app/contacts/edit/i15 => (app)\/(contacts)\/([^.]+)\/i([\d]+)
/app/contacts/view/i15-c2 => (app)\/(contacts)\/([^.]+)\/i([\d]+)-c([\d]+)
And for all of those I prepare different regexp like above. Problem stars when I need to parse something between:
/app/contacts/preview/random-name/i15-c2
or
/app/contacts/preview/random-name-i15-c2
([^.]+) catches also \ (slash) - and in above first example this slash broke whole regexp :-(
EDIT SECOND TIME :-)
I've got some predefined regexps for some paths. Lets have a look:
$regexp = [
[0 => '(app)\/([^/]+)'],
[1 => '(app)\/([^/]+)\/i([0-9]+)']
];
Next i check current url and match the best result - I know what I want, but php doesn't know what user select so:
if I enter url with path:
app/username/i15
System shows me that $regexp[0] is best result - what it is not true, because $regexp[1] are the best.
I hope this explains my problem.
Since you're using groups to capture in your regex, you can use them to create your desired array. Pass a variable to preg_match() to store the groups:
$components = [];
preg_match('#(app)/(module)/([^.]+)/i([0-9]+)#', $uri, $components);
Now the first element of $components is the whole match, and then the groups.
array_shift($components); # discard first element
Ok heres class for my routing:
https://gist.github.com/djmassive/d7487cef92e3adb1abf7ca31d3e96a53
Whole magic are done in method parse()
I've adding routings:
$route->get('(app)\/:string', [ 'schema' => '2', 'module' => 'system', 'file' => 'profile' ]);
$route->get('(app)\/:string\/i:id', [ 'schema' => '2,3', 'module' => 'system', 'file' => 'profile', 'command' => 'view' ]);
schema define what I want to be na id - it can be int, string or array.
module is folder name
file is filename
command is command use in file for show directly what I want
If I had url:
app/list - system reads it like that:
$command is list (schema shows second element) - works
app/username/i15
$command should be view
$id should be [ username, 15 ]
in this case, first regexp is showing 66% of match like second one - but second one should be 100%
Have a look at debuging:
Checking regexp: #(app)\/([^/]+)# - to match path: app/username/i15
Results preg_match \$matches array:
Array ( [0] => app/username [1] => app [2] => username )
Percentage match: 66.6666666667%
Checking regexp: #(app)\/([^/]+)\/i([\d]+)# - to match path: app/username/i15
Results preg_match \$matches array:
Array ( [0] => app/username/i15 [1] => app [2] => username [3] => 15 )
Percentage match: 66.6666666667%
we see, that first regexp are not good because there are not third element.
From what I know, the whole problem is mainly to not return what does not meet the full condition.
i am new on php and mongodb. i want to find all data which match the condition in php.
the following mongodb structure:
'Food' =>
array (size=4)
'_id' =>
object(MongoId)[36]
public '$id' => string '587e4a89974711036c97d3e5' (length=24)
'CaseId' => string 'c002' (length=4)
'Level' => string '4' (length=1)
'food' => string 'banana' (length=6)
i try to search 'food' value in Food object. i have do some research. i have some method to deal with. however, i cannot find anything. i don't know where wrong? hope someone can teach me.
here is my query:
$cursor=$collection->find(array("Food.food"=>array('$in'=>["banana"])));
$cursor=$collection->find(array('Food'=>array('food'=>"banana")));
$cursor=$collection->find(array('Food.food'=>"banana"));
after i research, i know MongoDB supports dot notation. resource from
enter link description here
also i find some get nested field method from
enter link description here
i following the link 2 to typing in mongodb compass. i can get the result i want, but when i follow the structure to enter in php. it have not any result.
what happen in these code?? i am confused. thank you.
i find other method to solve this problem. if someone meet this case like me, you can try below method.
$query= array(
'$match'=>array('Food.food'=>'banana')
);
$cursor=$collection->aggregate($query);
you can type $cursor['result'] to receive the searching result.
I have a string passed through a campaign source that looks like this:
/?source=SEARCH%20&utm_source=google&utm_medium=cpc&utm_term=<keyword/>&utm_content={creative}&utm_campaign=<campaign/>&cpao=111&cpca=<campaign/>&cpag=<group/>&kw=<mpl/>
when its present I need to cut this up and pass it through to our form handler so we can track our campaigns. I can check for it, hold its contents in a cookie and pass it throughout our site but i am having and issue using preg_match to cut this up and put it into variables so I can pass their values to the handler. I want the end product to look like:
$utm_source=google;
$utm_medium=cpc;
$utm_term=<keyword/>
there is no set number of characters, it could be Google, Bing etc, so i am trying to use preg_match to get the first part (utm_source) and stop past what I want (&) and so forth but I don't understand preg_match well enough to do this.
PHP should be parsing your query sting for you, into $_GET. Otherwise, PHP knows how to parse query strings. Don't use regular expressions or for this, use parse_str.
Input:
<?php
$str = "/?source=SEARCH%20&utm_source=google&utm_medium=cpc&utm_term=<keyword/>&utm_content={creative}&utm_campaign=<campaign/>&cpao=111&cpca=<campaign/>&cpag=<group/>&kw=<mpl/>";
$ar = array();
parse_str($str, $ar);
print_r($ar);
Output:
Array
(
[/?source] => SEARCH
[utm_source] => google
[utm_medium] => cpc
[utm_term] => <keyword/>
[utm_content] => {creative}
[utm_campaign] => <campaign/>
[cpao] => 111
[cpca] => <campaign/>
[cpag] => <group/>
[kw] => <mpl/>
)
I have created a very simple script to extract some parameters from the url using:
$_SERVER['QUERY_STRING'];
I have some values like this link:
http://test.com/r.php?gsgsg&0&1&0&sfs
The values are gsgsg 0 1 0 sfs.
Everything is fine when I use the link on own browser.
But when I press the link on a facebook post. I will only get:
gsgs 0 sfs
It seems like facebook removes my numbers.
I tried printing out the url I'm using to post the message in my app, all the values are there. But when I go in Facebook app and press the link, I am missing 2 int values.
Some one have a hint?
A query string like this:
gsgsg&0&1&0&sfs
Is interpreted as:
gsgsg=&0=&1=&0=&sfs=
Just from that you can already imagine that the value of the first 0 gets clobbered by the next. Why Facebook misses the 1 value is unclear, but it's common sense to apply variable naming rules to query string parameters.
I'm not sure what you're trying to do, but this would be much more reliable:
s[]=gsgsg&s[]=0&s[]=1&s[]=0&s[]=sfs
It generates an array $_GET['s'] of:
Array(
[0] => gsgsg
[1] => 0
[2] => 1
[3] => 0
[4] => sfs
)
For a site I am working on I use a library to get a list of states. It returns a numerically indexed array of states, each with three keys: stateCode, stateName, and stateSeg. It looks like this:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
array
'stateCode' => string 'AZ' (length=2)
'stateName' => string 'Arizona' (length=7)
'stateSeg' => string 'arizona-az' (length=10)
I often find myself with one of the three values and needing to look up its corresponding value. To do this I find myself constantly having to iterate through the array of states to find the data I need. Like this:
foreach ($this->data['stateList'] as $state)
{
if ($state['stateCode'] == $searchParams['state'])
{
$stateSeg = $state['stateSeg'];
break;
}
}
$url = BASEURL . '/' . $stateSeg . ".html";
This seems inefficient to me. I think the most efficient solution I’ve been able to come up with is to turn states into objects and put them in array with multiple keys for stateCode, stateSeg, and stateName each pointing to the same state object, so they can be referenced like this:
stateList[‘CA’]->getStateSeg();
or
stateList[‘Arizona’]->getStateCode();
or
stateList[‘alaska-ak’]->getStateName();
etc…
This also seems like kind of a hack which would result in a rather large array (150 keys pointing to 50 objects) with replicated data (keys replicating data stored within objects).
Anyway, just thought I’d see if there is some kind of pattern for this type of problem. This array of states isn't the only thing I’ve come across where I’ve had to do this sort of iterative searching on multidimensional arrays to find corresponding values.
Question is tagged PHP and the code above is in PHP, but I am interested in elegant solutions in any language.
If php supports references and I know the state, I'd just pass a reference to the appropriate array element and extract from it the necessary field.
Alternatively, if you never know in advance what state you can get, create and use a map (associative container/array), let its efficient implementation take care of quickly finding whatever you need. Seems like you may need several of them.
Also, I wonder if you could get rid of everything except the "alaska-ak" strings. The data appears highly redundant.
I think your basic idea with the object and the arrays is not that bad, but instead of creating actually objects, I would just refer to the existing objects (better: array data). Let's see your original list again:
array
0 => &
array
'stateCode' => string 'AL' (length=2)
'stateName' => string 'Alabama' (length=7)
'stateSeg' => string 'alabama-al' (length=10)
1 => &
array
'stateCode' => string 'AK' (length=2)
'stateName' => string 'Alaska' (length=6)
'stateSeg' => string 'alaska-ak' (length=9)
2 => &
...
Each state object has an identifier, the array key: 0, 1, 2, ... .
All you need to do is to create three indexes based on key. You use the value as key (e.g. "AL" for "stateCode" index) and as value you take the array index, 0:
$indexStateCode['AL'] = 0;
You can then already look this up quickly:
$states[$indexStateCode['AL']];
Encapsulate this into a class with ArrayAccess and then on request instantiate the state object. You don't need it earlier.
Could you store the states in a mysql/sqlite table and use the database engine to do the lookup?
This seems inefficient to me
It isn't. Even worse-case, iterating through 50 items is probably an order of magnitude faster than querying a db.
a library to get a list of states
Not sure why you'd need a library to do this. But I'd either change the library to return the array how you need it, or wrap it in another module.
The data is somewhat redundant... All you need is two items: the state code and the state name. You can construct the "state seg" from those two. So keep a state code map and a state name map.