new php programmer here, I apologize if this has already been asked:
I have an array filled with strings. Each string is the exact name of a page on my site, so the array is a sort of 'database'.
I'm wondering how to create a search engine that will let the user enter a keyword. The engine will then crawl my array of strings for any matches, and return a list of pages on my site that include the keyword in the pagename.
Any help is appreciated, not sure which functions to use to build this.
Thanks in advance!
I guess that you could just use strpos to see if the users term is contained inside of an array key. You would have to loop through each page, though, and see if the search term is contained within the pagename.
The way I would approach this is by creating a binary search tree with all the strings. That would make the search faster and more efficient. Here is a LINK that could help you achieve that.
Hope this helps.
I ended up solving this problem by using a flat file and conditionals, here is the code:
$userlink = $_POST['link_to_test'];
$linkdatabase = array('...');
if (in_array($userlink, $linkdatabase))
{
echo 'Data already exists';
}
else { ....
Related
I am currently using PHP to make a chatbot that can do research on wiki,and send the introduction automaitcally, here is the wiki api json(https://zh.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=search_title)
{"batchcomplete":"","query":{"redirects":[{"from":"search_title","to":"search_title"}],"pages":{"541":{"pageid":541,"ns":0,"title":"search_title","extract":"ablablablablablablablablablablabla。"}}}}
I want to get the extract part by
$data=$jsondata['query']['pages']["541"]['extract'];
but it seems like I need to have to know pageid is '541' first, is there method that I can know padeid in first??
You should look for a way to iterate over array values without knowing the keys. I'm not a PHP developer but I think a foreach will do:
foreach ($jsondata['query']['pages'] as $pageid => $pagedata) {
extract = $pagedata['extract']
...
}
There is also a formatversion=2 parameter for the query that, among other things, will make
action=query's "pages" be an array, instead of an object with page ids
as keys that can be difficult to iterate.
Can I get a quick reality check?
The idea that I'm working on is that I have text in a HTML template that looks like {Field1} and I want to populate the values for that field from a MySQLi connection where the database field is Field1.
So in theory I should get the data in an array, then cycle through that array, (adding {} and) replacing the text in the HTML with the required values.
Is there a more elegant solution to my task here or is what I'm planning pretty much it?
Does anyone have any advice or tutorials on handling the associated array returned by MySQLi? Much appreciated. I'm still learning and I'm just banging my head on this problem for a bit too long.
Without seeing your source for PHP, it is VERY difficult to answer this question,
I believe what you want to do can be achieved using *array_map*
Following would add the braces as you wish around each element in the array
// $row = .... your getter from mysqli connection
$row = array_map("apply_braces", $row);
// end of loops and stuff -- begin functions below
function apply_braces($field) {
return '{' . $field . '}';
}
The above will set all values in $row, to "{" . $somevalue . "}", so whatever the value of the item in the array is, it will add the braces around it using the custom function.
Sure.
Check failed.
Do not re-invent the wheel.
Your idea on templates is wrong. Please learn how to use already existing templating systems before trying to create your own.
Does anyone have any advice or tutorials on handling the associated array returned by MySQLi?
Definitely NO.
Simply because there is absolutely nothing special in the associative arrays returned from mysql. They are exactly the same as any other associative array in PHP, no difference.
I am trying to restrict content within a Wordpress template file and am using a plugin called Paid Memberships Pro to do so.
The code below restricts content to members with 'levels' of 1 or 2.
if(pmPro_hasMembershipLevel(array(1,2))){
restricted content goes here
}
The problem comes when I try to use a variable to provide the levels. These levels are held in a custom field group 'restrictions' with field name 'pmpro_id'. I access these levels within the template using...
foreach($restrictions as $restriction){
$temp=get_field('pmpro_id', $restriction->ID );
$temp_array[]=$temp;
}
$levels=implode(',', $temp_array);
If I then pass $levels to pmPro_hasMembershipLevel, this works fine if there is only one level but fails if there are 2 or more. I believe this is because the variable type is then a string rather than integer? I had previously tried to pass the $temp_array directly though I felt this wouldn't work and was correct.
I realise this is probably PHP 101. I have searched but don't really know what I'm looking for to be honest! I am not a developer and this is the last thing holding me back from finishing this project so ANY help anyone could provide would be brilliant. Thanks in advance.
You don't need to implode $temp_array if pmPro_hasMembershipLevel accepts array as its argument. When you implode an array you get string as a return value — that's not what you want here. If you think that the issue might be with the type of values, then you can try to cast them to integers, like this $temp_array[]= (int) $temp;
I have a long PHP file and I want to copy all the variable names only and build an insert sql query. Is there a way where I can search for a pattern using regular expression and concatenate the find result till I collected all the variable and spit it out in a statement?
I am using TextMate and am familiar with regular expression search. Regex search result give $0,$1 and so forth argument. Do not know if this possible though. Solution in any editor will do not just text mate.
I have just too many variable (+100) don't feel like copy every single one. Here my sample file
$ID = $_POST['id'];
$TXN_TYPE = $_POST['txn_type'];
$CHARSET = $_POST['charset']
$CUSTOM = $_POST['custom'];
You could try something with get_defined_vars(). However this function also lists GLOBAL vars. You can use this snippet to remove them if you don't want them and display only the vars you defined
$variables = array_diff(get_defined_vars(), array(array()));
However this snippet generates Notices and I haven't found a way to solve them yet.
If you've only got $_POST variables you can loop through the $_POST array itself
You create the SQL programmatically while looping through the array.
My own solution is, do the inverse. It is not probably possible.
Leave only the variable names Remove all the rest. Use
[space].+ regex to remove everything that is after the variable name.
clean the file so that only variable names are left. then do a couple more find and replace to bring the variable name in the form you want.
If you're looking to match only the variable names (not the $_POST array indices), then the regular expression is pretty much provided in the PHP documentation:
\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
This will, of course, include $_POST, but that should be easy enough to remove. If not, you could do it with negative lookahead (if TextMate supports it):
\$(?!_POST($|[^a-zA-Z0-9_\x7f-\xff]))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
I'm working on a project where all of the members and their info are stored in a JSON file. I'm in the process of creating a search form and I need help on how to iterate through the members and check to see if there's an exact match or a similar match.
The members are stored in a SESSION variable:
$_SESSION['members'] = json_decode($jsonFile);
but I'm uncertain how to use regex to check for matches that are similar (and not just exact). For example, if a member's name is "Jonathan", I'd like that result to be returned even if the user searches "Jon". Is regex the correct approach? Any help will be greatly appreciated - thank you!
-Manoj
I think I'd be using a database to store the data rather than JSON so that you can use the LIKE searches, e.g.
SELECT * FROM users WHERE name LIKE 'Jon%'
If you absolutely have to use JSON you could loop through all members and use a regexp like
preg_match('/^'.$term.'.*/i', $element, $matches);
to check them all.
If the $jsonFile contents is an array of some sort, you may find preg_grep() of use, though it doesn't work on multidimensional arrays. You might have have to loop over each individual member record and grep the relevant fields yourself, something like:
foreach ($_SESSION['members'] as $idx => $member) {
... match relevant fields...
}