mask data id card number in php - php

I have some data resembling the following:
444444444444444
I want to add a mask to the data so it looks like this:
44.444.444.4-444.444
I tried using:
$x_data = preg_replace('~.{3}\K(?!$)~', '.', $x_data);
Which got me:
444.444.444.444.444
How do I modify my preg_replace to give me the exact output I am looking for?

You could use a capture group approach, and then connect the subcomponents according to the format you are expecting.
$x_data = "444444444444444";
echo $x_data . "\n";
$x_data = preg_replace('/^(\d{2})(\d{3})(\d{3})(\d)(\d{3})(\d{3})$/',
'$1.$2.$3.$4-$5.$6',
$x_data);
echo $x_data;
This prints:
444444444444444
44.444.444.4-444.444

Related

converting date value from html form with dashes in wrong order to slashes in correct order

I have an html form that I am posting a date value in the following format:
2019-12-13
What I need (due to the software it is being imported into) is to format it like:
12/13/2019
what I tried:
$dateUSD = date($_POST['dateUSA'])->format('dd/mm/yyyy');
it did not work..
also trying to just change the - to /:
$dateUSD = $_POST['dateUSA'];
//2019-12-12
$dateUSD = str_replace('-', '/', $dateUSD);
But htat is not working either.
Just try this :
$date="2019-12-13";
echo date('d/m/Y',strtotime($date));
Output :
13/12/2019
Try this :
$date=date_create("2019-12-13");
echo date_format($date,"d/m/Y");
Result :
13/12/2019

How to use echo in url

i am trying to use echo inside url. i have store data from the form in database and now i am also fetching it on my page and its working well. Now i am trying to print that data i.e. number and date in url.
Is it possible and if possible please help me out
here is my data that i am fetching and it prints the output
echo $number;
echo $yyyymmdd;
and here is my url in which i want to insert ' echo $number; ' and ' echo $yyyymmdd; ' on the place of and .
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/<number>/date/<yyyymmdd>/");
I have also tried something like this but it gives error of syntex error.
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/"echo $number;"/date/"echo $yyyymmdd;"/");
Another way to add changing parameters to a URL (or string) is by using sprintf(). You define your URL and a type specifier like %d as a placeholder for numbers, and %s for strings. See the php doc for the full list of type specifiers.
$urlFormat = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/%d/date/%s/"
^ ^
Then call sprintf with the changing parameters in order of appearance.
$url = sprintf($urlFormat, $number, $yyyymmdd);
$json = file_get_contents($url);
This becomes more convenient especially if you are calling file get contents in a loop.
Create two variables and append those two inside double-quote or single quote, depending upon the quotes which you have opened and close it.
<?php
$number=123;
$yyyymmdd='2018-10-9';
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/".$number."/<number>/date/<".$yyyymmdd.">/");
?>
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/");
When you compose text, you do not need "echo" but just can write variable.
You can directly use variables in double quotes like this
file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/");
Sample code below
$number = 344;
$yyyymmdd = "20180301";
$url1 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/";
echo "url1 ".$url1."\n";
$url2 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/";
echo "url2 ".$url2. "\n";

Add method return values together

I'm working on a project that uses the Facebook API. Im using methods from that API to automaticly create a username. But for that I need to add the firstname and the lastname.
The way on how I retrieve the firstname and lastnames is:
$userNode->getLastName();
$userNode->getFirstName();
How do I add those return values togheter?
If I do this:
$string = $userNode->getFirstName() + $userNode->getLastName();
echo "$string";
the echo shows "0".
Thanks in advance!
Mats de Waard
The first string operators example in the manual will show you:
$string = $userNode->getFirstName() . $userNode->getLastName();
You need to make a concatenation in php
in your example use "." instead of "+"
$string = $userNode->getFirstName() . $userNode->getLastName();

How can I sanitise the explode() function to extract only the marker I require?

I have some php code that extracts a web address. The object I have extracted is of the form:
WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults
Now in PHP I have called this object $linkHREF
I want to extract the id element only and put it into an array (I'm bootstrapping this process to get multiple id's)
So the command is:
$detailPagePathArray = explode("id=",$linkHREF); #Array
Now the problem is the output of this includes what comes after the id tag, so the output looks like:
echo $detailPagePathArray[0] = WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&w
echo $detailPagePathArray[1] = bf&page=1&
echo $detailPagePathArray[2] = 16123012&source=searchresults
Now the problem is obvious, where it'd firstly picking up the "id" in the "wid" marker and cutting it there, however the secondary problem is it's also picking up all the material after the actual "id". I'm just interested in picking up "16123012".
Can you please explain how I can modify my explode command to point it to the particular marker I'm interested in?
Thanks.
Use the built-in functions provided for the purpose.
For example:
<?php
$url = 'http://www.example.com?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults';
$qs = parse_url($url);
parse_str($qs['query'], $vars);
$id = $vars['id'];
echo $id; // 16123012
?>
References:
parse_url()
parse_str()
if you are sure that you are getting &id=123456 only once in your object, then below
$linkHREF = "WEBSITE?flage=2&fgast=48&frat=1&sort=D&fsrc=2&wid=bf&page=1&id=16123012&source=searchresults";
$str = current(explode('&',end(explode('&id', $linkHREF,2))));
echo "id" .$str; //output id = 16123012

PHP GET number query

Hi to all!
I have a query that gets the name and an id.
The results is like this :
54 - Rian Ree Barrientos
I wanted to get the number 54.
I used echo (int)$_GET['number'];
But the result is "0". How can I get the number?
You can't get it that way, you need to make two query string vars for it eg:
<a href="somepage.php?number=54&str=some_string">
Make sure that if you do so, you use the urlencode function.
Now you can use:
echo (int) $_GET['number'];
And:
echo $_GET['str'];
Otherise you can use the explode function to get the two values by specifying the - delimiter.
I think you want id from the value you get from the query
I think you want something like following
$strPrice1 = mysql_query("SELECT id, name FROM table_name where id=".$_GET['id'] );
$strPrice = mysql_fetch_array($strPrice1);
echo (int)$_strPrice['id'];
But the result is "0". How can I get the number?
Really?
When I run:
print (int)('54 - Rian Ree Barrientos') . "\n";
I get 54
(php v 5.1.6)
Maybe $_GET['number'] doesn't contain what you think.
C.

Categories