How to get my word from following string? - php

I have two strings in PHP:
$Str1 = "/welcome/files/birthday.php?business_id=0";
$Str2 = "/welcome/index.php?page=birthday";
I have to get word birthday from this two string with a single function.
I need a function which returns birthday on both case.
example
function getBaseWord($word){
...
return $base_word;
}
getBaseWord('/welcome/files/birthday.php?business_id=0');
getBaseWord('/welcome/index.php?page=birthday');
both function call should return "birthday".
How can i do it.

If I correctly understand what you are trying to do then this should do what you need:
function getWord(){
return $_GET['page'];
}
or $_GET['business_id'];
I think $_GET is an associative array made from the GET request that was sent to the page. An associative array is one where you access something like ['name of the element'] instead of [1] or [2] or whatever.

So what you will need to do is get all of the actual GET variables extracted from the string:
//Seperate the URL and the GET Data
list($url,$querystring) = explode('?', $string, 2);
//Seperate the Variable name from its value
list($GETName, $GETValue) = explode("=", $querystring);
//Check to see if we have the right variable name
if($GETName == "page"){
//Return the value of that variable.
return $GETValue;
}
NOTE
This is very BASIC and will not accept more then one GET parameter. You will need to modify it if you plan on have more variables.

I have no idea what you are talking about but, you can cut the word "birthday" with str_replace and replace with another word, or you can find the position with stripos, I have no idea what we are trying to do here, so those are the only things come to my mind

Related

Get the value from URL

In this URL: www.example.com/transaction/summary/10 I can get 10 by this:
$this->uri->segment(3);
But, along with some other GET params how can I get that value?
e.g.: www.example.com/transaction/summary?local_branch=1/10
PS: GET params could be more than 1.
If you still want to keep CI style than add segments:
URL:
www.example.com/transaction/summary/10/1/TEST
Segments:
$this->uri->segment(3); // 10
$this->uri->segment(4); // 1
$this->uri->segment(5); // TEST
Or if you want to use query string than you can add params in query string and get values by using $_GET:
www.example.com/transaction/summary/10/?local_branch=1&test=test
Ok, segments in Codeigniter are the content between slashs like mysite.com/page/page-name. To get the page-name value I get the second segment, or $this->uri->segment(1). To get the strings passed by query string ($_GET), you can simply use the $_GET['local_branch'] as in your example or in case of Codeigniter, as the #Tom answer: $this->input->get('local_branch').
In this case, you can to explode the values delimiting by /.
$localBranch = $_GET['local_branch'];
// Or
$localBranch = $this->input->get('local_branch');
// And split using the / as delimiter if is need.
$localBranch = explode('/', $localBranch);
// Output
// Array(0 => 1, 1 => 10)
This way many values can be passed in same query string.
To get these GET parameters you can just use:
$this->input->get('some_variable', TRUE);
As per the SO link here.
example.com/?some_variable=hey

PHP Find a 15 or 16-digit number in a long string

I am working on a function to sanitize credit card data by replacing all digits except the last 4 with X's. the problem is that I have a lot of data already stored in the database as json objects. each record may or may not have a card number somewhere in it, and they occur in different places in the text from record to record (sometimes at the very end, sometimes in the middle, etc.) Looking for a way to extract all 15-digit or 16-digit numbers from the string so that I can pass this number to the sanitize function.
Looking for a way to extract all 15-digit or 16-digit numbers from the string
You can use:
\b[0-9]{15,16}\b
In PHP You can use like this:
<?php
$subject="aa999999999999999aa888888888888888as555555ssd555fs22aa999999999999999a";//Your string
preg_match_all('/(?:[0-9]{15,16})+/s', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
print_r($result);//Printing result
output:
Array ( [0] => 999999999999999 [1] => 888888888888888 [2] => 999999999999999 )
if you want only one and first number then you can use this instead:
if (preg_match('/([0-9]{15,16})/s', $subject, $regs)) {
$result = $regs[0];
} else {
$result = "";
}
If it is stored as a json object in the DB and you want to find an attribute in the object you'll want to use json_decode() to take the record and make it a php Stdclass.
so if the json record looks like this:
$record = "{ 'name' : 'theSpurg', 'cc_num' : '139764518623' }"
You can access it like this
$test = json_decode($record);
$credi_num = $test->cc_num;
if that's not what you're looking for then I am misunderstanding the situation.
Cheers

db table names as alias in php GET-paramter?

I am looking for a possibility to pass db table name and column name via php and GET parameter.
I have a data grid with following structure:
table_name1.column1, table_name2.column1, table_name2.column1.
There is a search function for the grid, where I need those parameters.
From the url "?table_name1.column1=22" I am getting through the $_GET only table_name1_column1=22
How would you solve that?
Encode the variable with base64 and decode before you use.
I know that's dirty.
But php variables doesn't support periods (dots)
This is a documented feature of PHP. Its basically because PHP cannot have variable names with dots in them.
$x.y = 1 // is an invalid variable name
MANUAL Convert dots to _ in GET & POST
You can use serialize function of php to pass the text "table_name1.column1" in url. But for that you need to do few coding to get the result. Below I have given code which you can use:
Page1.php
<?php
$test = serialize('table_name1.column1=11::table_name2.column2=22');
?>
<a href='Page2.php?qs=<?php echo $test?>'>test</a>
Use this $test to pass as query string. In above example, I am passing on anchor tag click.
Page2.php
use following code to Unserialize the query string and use the values.
<?php
$test2 = unserialize($_GET['qs']);
$ex = explode('::',$test2);
$new_arr = array();
foreach($ex as $val)
{
$ex2 = explode('=',$val);
$new_arr[$ex2[0]] = $ex2[1];
}
echo $new_arr['table_name1.column1']; //print 11
echo $new_arr['table_name2.column2']; //print 22
?>
Hope this will help you :)
You need to seperate each parameter with &.
So: ?table=table_name1&column=column1

php issue accessing array

I have the following code :
$results = $Q->get_posts($args);
foreach ($results as $r) {
print $r['trackArtist'];
}
This is the output :
["SOUL MINORITY"]
["INLAND KNIGHTS"]
["DUKY","LOQUACE"]
My question is, if trackArtist is an array, why can't I run the implode function like this :
$artistString = implode(" , ", $r['trackArtist']);
Thanks
UPDATE :
Yes, it is a string indeed, but from the other side it leaves as an array so I assumed it arrives as an array here also.
There must be some processing done in the back.
Any idea how I can extract the information, for example from :
["DUKY","LOQUACE"]
to get :
DUKY, LOQUACE
Thanks for your time
It's probably a JSON string. You can do this to get the desired result:
$a = json_decode($r['trackArtist']); // turns your string into an array
$artistString = implode(', ', $a); // now you can use implode
It looks like it's not actually an array; it's the string '["DUKY","LOQUACE"]' An array would be printed as Array. You can confirm this with:
var_dump($r['trackArtist']);
To me content of $r['trackArtist'] is NOT an array. Just regular string or object. Instead of print use print_r() or var_dump() to figure this out and then adjust your code to work correctly with the type of object it really is.

How to grab a value from a url with multiple values using PHP. [2 Part Question]

This is a two part question. First how can I grab the last url value from a link when I dont know how deep the value is, for example how can I grab
the last value of sub_4 from the link example below using PHP? And second how can I grab the url value cat=3 and the last url value sub_4 using PHP?
I'm kind of new to PHP so a detailed step by step example would help me out a lot if its not too much trouble.
Thanks in advance!
Here is an example of a URL value.
http://www.example.com/categories/index.php?cat=3&sub_1=sub1&sub_2=sub2&sub_3=sub3&sub_4=sub4
All variables from the hook will be returned on $_GET. So if you want to get that value from the URL, just use:
$_GET['sub_4']
If you want to get a list of all of the possible values:
array_keys($_GET)
This will give you all of the variables.
UPDATE: I just reread your question. I think I better understand what you are looking for. Correct me if I am wrong, but you are not certain how to get the last element of the string? Is that right? So you could potentially have sub_5, sub_6, sub_7 etc. Here is how to get the last element from the string:
end($_GET);
$key = key($_GET);
$last_item = $_GET[$key];
$last_item will now have sub_4 (or what ever the last item is).
Extracting URL Parameters
Query parameters (anything in the URL with ?name or &name) are saved in the $_GET superglobal.
If sub is a hierarchy, you should probably just write it as such. For instance: ?sub=path/to/sub or ?sub=path:to:sub
From this you can explode() on your separator (/ or :) to get the different parts of the sub parameter. $sub_array = explode('/', $_GET['sub']);
You can then iterate over the array using a foreach or directly access the highest branch of the hierarchy using count():
$sub_array[count($sub_array-1)];
Building URL Parameters
If you have an array of subs that you want to use to generate a URL, you can use implode() to build your URL params. $sub_params = implode('/', $_GET['sub']);
You might construct that array by appending each sub to the $sub_array.
$sub_array[] = 'sub1';
$sub_array[] = 'sub2';
$sub_array[] = 'sub3';
etc.
Inspect the Data
If you get lost, use var_dump($_GET) or var_dump($variable) to see what's inside it.
Each cat=3 or sub_1 value can be retreived by $_GET['cat'] and $_GET['sub_1'] respectively. To elaborate, $_GET['NAME_OF_PROPERTY'] would look like php.net/index.php?NAME_OF_PROPERTY=whatever
It seems like both questions were on how to grab the value from the URL, so I hope that answers both.
In your example url http://www.example.com/categories/index.php?cat=3&sub_1=sub1&sub_2=sub2&sub_3=sub3&sub_4=sub4
echo $_GET['cat']; // 3
echo $_GET['sub_1']; // sub1
echo $_GET['sub_2']; // sub2
echo $_GET['sub_3']; // sub3
echo $_GET['sub_4']; // sub4
For the first part; you should use the $_GET[] array which contain every parameter passed in the url $_GET['cat'], $_GET['sub_1'].
For the second part, in your case you should try send an array in parameter like this :
http://www.example.com/categories/index.php?cat=3&sub[1]=sub1&sub[2]=sub2&sub[3]=sub3&sub[4]=sub4
And then use the $_GET['sub'][] array $_GET['sub'][1], $_GET['sub'][2], ...
Now you can determine the length of the array and know the last item in it.
This example is under the assumption you want to find the very first variable in the GET query, and the very last:
<?php
// Here, you separate all the variables
$parameters = explode("&", $_SERVER['QUERY_STRING']);
// Get the last key's index
$last_key_index = count($parameters)-1;
// Easy way for PHP 5.3
$first_key = strstr($parameters[0], "=", true);
$last_key = strstr($parameters[$last_key_index], "=", true);
// Bit longer otherwise
$first_key = substr($parameters[0], 0, strpos($parameters[0], "="));
$last_key = substr($parameters[$last_key_index], 0, strpos($parameters[$last_key_index], "="));
// Test it here
echo($first_key);
echo($last_key);
?>

Categories