Here I have an input example:
When user enters formate like {login}:{ip}:{password}
or {ip}|{:login}
any formate, PHP gets from database these datas and add them to the txt file and the user downloads it.
Here is a result should look like:
How to do it?
I tried this:
$text = '{login}:{password}:{ip}';
preg_match_all('/{[^}]*}/', $text, $matches);
$return = str_replace($matches[0], "", $text);
$splitter = substr($return, 0,1);
But it doesn’t works
2
Answers
If I understand it correctly,
login
,password
andip
are database field names. So you could only accept valid chars for DB field names instead of[^}]*
, which might be too lax.You could start off with
/{([a-z_]w*)}/gi
instead.Test it here: https://regex101.com/r/4hg0aC/1
The second step would be to validate that these field names really exist in
your database table. If the DB doesn’t change, then you could have two ways to
achieve it:
/{(login|password|ip)}/gi
and then doa
strtolower()
of the matched group.
could even be the result of a
SQL query to get the field names.
Then build your DB request with PDO to get
the data.
If you already have a variable containing the fields you want to expose, then
you could simply check that the property exists or not.
Example of PHP code:
You can test it here: https://onlinephp.io/c/71e2d