Comparing variable pathinfo - php

How do I get the path info (php) when the path is of the form: www.example.com/post/* (* being a variable number that matches the post id) and compare it to www.example.com/post to make it TRUE.
In other words, if pathinfo is www.example.com/post/1068 (or 1069, or 1070, or whatever the id number) then the statements that follow are executed. If not, then they are skipped.
The snippet is destined to a template file. If this requires a pre-process function, please say so. Thank you.
I am new at this so please do not send me to some other post that reads like Chinese! :)
Thank you.

You could explode and filter for empty values and then use the last occurrence, something rough like this:
$url = "www.example.com/post/1068";
$id = array_pop(array_filter(explode('/', $url)));
$validIds = array("1068", "1069", "1070");
if (in_array($id, $validIds)) {
//do something;
}
explode will separate your string in an array using "/" as a separator (in this case)
array_filter will filter empty values in order to make sure that the last value will be your id
array_pop will retreive the last value of the given array
in_array will compare a value with an array and check if the value is within the given array and return true/false depending whether it is or not inside the array.

Related

keep adding up code from database

# Check arrary not empty
if (!empty($results)) {
$this->code($results);
// got the mail code from database
// which is PG-000001
// how do i add , like something PG-000001 ++
}
this will return a result from database , my intention is to keep adding up the code that return from my database and the update back to the database.
now it was return PG-000001, how do i make it add up and be like PG-000002 and then update it and next time it will be PG-000002 and up to 000003 and so on.
how do i add up the text PG-000001?
If all of your codes look like this, then your really shouldn’t store them that way. It appears that the PG- at the beginning is just a prefix. If you store the actual value as an integer, you can increment as much as you like.
Anyway, the solution to your question is that you will need to
split the string
increment the second part
zero-pad the second part
combine again
Here is a little test script:
$test='PG-000001';
$pattern='/(.*-)(\d+)/';
preg_match($pattern,$test,$matches);
list(,$prefix,$value)=$matches;
$value=sprintf('%06d',$value+1);
$test="$prefix$value";
print $test;
Translation:
/(.*-)(\d+)/ is the pattern that will split the string into the prefix & numeral
preg_match applies the pattern and returns the result into the array $matches.
$matches has the original string, and then the two matches
list() copies elements of the array into variables. The leading comma skips the first element
sprintf formats the data. In this case, the code 0-pads to 6 digits
the double-quoted string is a simple way of recombining your data.

Query Based on User Input—PHP

A script I'm using takes in a string of field name/value pairs, splits them, and creates a query from them. The string is formatted like this:
var1==value1,var2==value2...
The values will be submitted by users on the frontend of the website. So, if a user selects a value for var1 and var4 but not 2 and 3 I would need the string to look like this:
var1==value1,var4==value4
Getting the user-submitted data isn't a problem. What is the best way to add in the field name and == only if the associated value is not blank?
if (isset($varname))
isset — Determine if a variable is set and is not NULL
if(empty($varname)))
empty — Determine whether a variable is empty
if(is_null($varname)))
is_null — Finds whether a variable is NULL
In other words, it only returns true when the variable is null. is_null() is the opposite of isset(), except you can use isset() on unknown variables.
You could do something like
$pairs = "var1==stuff,var3==morestuff"
if(strpos($pairs, "var2==") !== false){
$pairs .= "var2==defaultvalue";
}
And you could do that for every var# you want. This would be able to check if the
About the strpos : How do I check if a string contains a specific word in PHP?
Just whip over the submitted form fields looking for anything with a value:
foreach($_POST as $key => $value){
if($value != ''){
// do stuff
}
}
also add a check to skip the submit = submit bit :)

Replace custom tag inside a string with value from database

what I need to do is to parse a string similar to this:
{a}3{/a}*{b}4{/b}
or this
{a}3{/a}/{b}2{/b}*100
I need to substitute in that string those values within the tags with real values from the database, the first example:
SELECT value FROM table WHERE id = 3;
SELECT value FROM table WHERE id = 4;
This function:
preg_replace_callback('/(?>{([^}]+)})(.*)?{\/\1}/sim', 'find_tags_callback', $string);
Actually returns the ids contained in the string, the problem is that I'm stuck there. In pseudo code I would need to:
Extract the first id from the string.
Run my query.
Substitute that id with the correct value.
[Do the same for all tags]
Finish having back the initial string with the correct value inside.
The first might be
10*3
the second
40/90*100
Any idea how to do this, I'm completely stuck.
Thanks
Do a database query to get all the values, and put them into an array keyed off the IDs. In the code below, I assume the array is named $tags.
$new_string = preg_replace_callback('/\{([^}]+)\}(.+?)\{/\1\}/sim',
function ($match) use ($tags) {
return $tags[$match[2]];
}, $string);
The use ($tags) declaration allows the function to reference the external variable $tags.

My array isn't recognized as array when it contains only one value

In my code, I fill an array using this line in a loop :
$_SESSION['my_array'][] = $some_value;
After each execution of this line, I do some check (doesn't matter here for which purpose) using the function in_array(). However, at the first iteration it says :
« in_array() expects parameter 2 to be array ».
How to fix this problem?
You can initialize the array (before you fill it with values) like this:
$_SESSION['my_array']=array();
This way you can be sure that it is array, even when it would be empty.
You are assigning or accesing it wrongly
Use this
$_SESSION['my_array'] = $some_value;
When you are doing the in_array check, you can cast the second item to an array, so if it's empty then it will pass an empty array. This way you don't ever set anything to the session when you don't need to (which could trip you up later on)
e.g.,
if (in_array('foo', (array)$_SESSION['my_array'])) {
// do something
}

Better way to get first element of returned array

I found myself using this:
$var=(string)array_shift(array_values($item->xpath($s)));
where $s is an xpath search string and the return is an array of objects that contain strings.
It works, but I'm not sure it's the best way to get the data I want.
I could use a tempvar, but I wanted to avoid that.
Any suggestions?
Careful with array_shift, as it will remove the element from the array stack, if you simply want the first value, you can use current:
$var = (string) current($item->xpath($s));
I believe this gives the same result.
$var=array_shift($item->xpath($s));
$var = $reset($item->xpath($s));
Note that this rewinds the array's internal pointer and returns the first element. The method current returns the element at the position the pointer happens to be in - it is not guaranteed to always be the first element.

Categories