//Chars to be replaced
$suchmuster = array();
$suchmuster[0] = 'á';
$suchmuster[1] = 'à';
$suchmuster[2] = 'â';
$suchmuster[3] = 'é';
$suchmuster[4] = 'è';
$suchmuster[5] = 'ê';
$suchmuster[6] = 'í';
$suchmuster[7] = 'ì';
$suchmuster[8] = 'î';
$suchmuster[9] = 'ó';
$suchmuster[10] = 'ò';
$suchmuster[11] = '/ô/';
$suchmuster[12] = 'ú';
$suchmuster[13] = 'ù';
$suchmuster[14] = 'û';
$suchmuster[15] = ' ';
$suchmuster[16] = '.';
$suchmuster[17] = ',';
$suchmuster[18] = '-';
$suchmuster[19] = '_';
//Replaces
$ersetzungen = array();
$ersetzungen[0] = 'a';
$ersetzungen[1] = 'a';
$ersetzungen[2] = 'a';
$ersetzungen[3] = 'e';
$ersetzungen[4] = 'e';
$ersetzungen[5] = 'e';
$ersetzungen[6] = 'i';
$ersetzungen[7] = 'i';
$ersetzungen[8] = 'i';
$ersetzungen[9] = 'o';
$ersetzungen[10] = 'o';
$ersetzungen[11] = 'o';
$ersetzungen[12] = 'u';
$ersetzungen[13] = 'u';
$ersetzungen[14] = 'u';
$ersetzungen[15] = '';
$ersetzungen[16] = '';
$ersetzungen[17] = '';
$ersetzungen[18] = '';
$ersetzungen[19] = '';
$newmessage = preg_replace($suchmuster, $ersetzungen, strtolower($message));
The above code was created to replace some special chars with normal ones but no matter which $nachricht (= $message) I put in, it always gives back an empty string!
Thanks for your help.
EDIT
I Changed it to:
//Chars to be replaced
$suchmuster[0] = '/ /';
$suchmuster[1] = '/./';
$suchmuster[2] = '/,/';
$suchmuster[3] = '/-/';
$suchmuster[4] = '/_/';
//Replaces
$ersetzungen[0] = '';
$ersetzungen[1] = '';
$ersetzungen[2] = '';
$ersetzungen[3] = '';
$ersetzungen[4] = '';
$newmessage = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', strtolower($message));
$newmessage = preg_replace($suchmuster, $ersetzungen, $newmessage);
It still gives back an empty string.
Instead of using several patterns, you can also combine the pattern to:
$nachrichtneu = preg_replace('/[,.\s_-]/', '', $nachrichtneu);
And strtolower will not convert Á and the like to á. First do the iconv conversion, then the strtolower.
Regarding your updated question:
Regular expressions use special control characters. In your case it is /./ which causes your result to be empty. . means "any character" in regex, so you are replacing each and every character with nothing.
use preg_quote() to escape special characters in regular expressions. Example: $xy = '/' . preg_quote('.') . '/';
But you can do this much simpler without using regular expressions, like:
$replaces = array(
'.' => '',
',' => '',
// ...
);
$message = str_replace(array_keys($replaces), array_values($replaces), $message);
change your code through
$suchmuster = array();
$suchmuster[0] = "/á/";
$suchmuster[1] = "/à/";
...
Related
A shipping service is providing me with this weird text format response, after some research i found it is very close to a .net array and i am trying to convert to PHP so i can do something useful with it.
My questions:
What is this format?
Is there any PHP function i could use to parse it?
City[0] = "Elghorashi";
State[0] = "";
ZipCode[0] = "";
CCity[0] = "Elghorashi";
CState[0] = "";
CZipCode[0] = "";
City[1] = "Abugibha";
State[1] = "";
ZipCode[1] = "";
CCity[1] = "Abugibha";
CState[1] = "";
CZipCode[1] = "";
Thanks
Here's a quick little snippit I whipped up to convert a string that looks like a PHP array into an actual PHP array. To be clear the $newArray variable is where the array is created to.
$string = 'City[0] = "Elghorashi"; State[0] = ""; ZipCode[0] = ""; CCity[0] = "Elghorashi"; CState[0] = ""; CZipCode[0] = ""; City[1] = "Abugibha"; State[1] = ""; ZipCode[1] = ""; CCity[1] = "Abugibha"; CState[1] = ""; CZipCode[1] = "";';
$string = substr($string, 0, -1);
$arr = explode('; ', $string);
$newArray = [];
foreach($arr AS $value){
$keyVals = explode(' = ', $value);
preg_match_all('/^(.*?)\[([0-9]+)\]$/', $keyVals[0], $matches);
$value = is_string($keyVals[1]) ? substr($keyVals[1], 1, -1) : $keyVals[1];
$newArray[$matches[2][0]][$matches[1][0]] = $value;
}
Now, this isn't a "debug my codez for me plz" question. I've spent... I don't even want to think how long on trying to fix this issue.
My problem is that I am executing this php page that is, ideally, examining a string and converting it to a more secure format since this page is to store passwords for accounts.
What I'm aiming at with this code below is to slice the string repeatedly at each character and then evaluate it to my key. It does this for the whole string length. What's been being returned to me is '0'. I don't know how the system is even getting this value.
Maybe I'm using the substr() function wrong? Also, I'm open to a completely different method of parsing the string, such as using a RegExp. Thank you for your help, guys!
Code:
<?php
error_reporting(0);
#region Initial
$accstr = "apple";
$accn = "scourge";
//Key
$a[0] = "#"; //These variables are for conversion; indexes of this array correspond to those
$a[1] = "!"; //of the other array ($ind)
$a[2] = "#";
$a[3] = "%$";
$a[4] = "%";
$a[5] = "!#";
$a[6] = "&*";
$a[7] = "^";
$a[8] = "##";
$a[9] = "&^";
$a[10] = "&";
$a[11] = "%~";
$a[12] = "!%";
$a[13] = "!$";
$a[14] = "*#";
$a[15] = "#*";
$a[16] = "~";
$a[17] = "~&";
$a[18] = "``";
$a[19] = "/^";
$a[20] = "%`";
$a[21] = "~~";
$a[22] = "`~";
$a[23] = "%%";
$a[24] = "~!";
$a[25] = "~#";
$a[26] = "``#";
$a[27] = "``!";
$a[28] = "``#";
$a[29] = "``%$";
$a[30] = "``%";
$a[31] = "``!#";
$a[32] = "``&*";
$a[33] = "``^";
$a[34] = "``##";
$a[35] = "``&^";
$a[36] = "&&^#";
$a[37] = "~#!";
$a[38] = "!#&#";
$a[39] = "%~~$";
$a[40] = "%`%";
$a[41] = "!^~#";
$a[42] = "&#$*";
$a[43] = "^**&";
$a[44] = "#%#`";
$a[45] = "&``!#^";
$a[46] = "&**~&";
$a[47] = "%|~";
$a[48] = "!-|~%";
$a[49] = "!$~";
$a[50] = "*/#";
$a[51] = "#%*";
$a[52] = "|~";
$ind[0] = "a";//These are used to tell what's being looked at in the string
$ind[1] = "b";
$ind[2] = "c";
$ind[3] = "d";
$ind[4] = "e";
$ind[5] = "f";
$ind[6] = "g";
$ind[7] = "h";
$ind[8] = "i";
$ind[9] = "j";
$ind[10] = "k";
$ind[11] = "l";
$ind[12] = "m";
$ind[13] = "n";
$ind[14] = "o";
$ind[15] = "p";
$ind[16] = "q";
$ind[17] = "r";
$ind[18] = "s";
$ind[19] = "t";
$ind[20] = "u";
$ind[21] = "v";
$ind[22] = "w";
$ind[23] = "x";
$ind[24] = "y";
$ind[25] = "z";
$ind[26] = "0";
$ind[27] = "1";
$ind[28] = "2";
$ind[29] = "3";
$ind[30] = "4";
$ind[31] = "5";
$ind[32] = "6";
$ind[33] = "7";
$ind[34] = "8";
$ind[35] = "9";
$ind[36] = "~";
$ind[37] = "!";
$ind[38] = "#";
$ind[39] = "#";
$ind[40] = "$";
$ind[41] = "%";
$ind[42] = "^";
$ind[43] = "&";
$ind[44] = "*";
$ind[45] = "(";
$ind[46] = ")";
$ind[47] = "_";
$ind[48] = "+";
$ind[49] = "`";
$ind[50] = "-";
$ind[51] = "=";
$ind[52] = "?";
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$finln = "";
#endregion
#region Create coded password
$pwlen = strlen($accstr);
for($cnter=1;$cnter<=$pwlen;$cnter++)
{
$a1 = substr($accstr,$cnter,1);
for($cnter2=1;$cnter2<=52;$cnter2++)
{
if($a1==$ind[$cnter2])
{
$finln += $a[$cnter2];
}
}
}
#endregion
#region Send finln
$newpw = $xml->createElement($accn);
$newpw->appendChild($xml->createElement('password', $finln));
$xml->getElementsByTagName('cache')->item(0)->appendChild($newpw);
file_put_contents("pwDB.xml",$xml->saveXML());
print $finln;
#endregion
?>
So typical password hashing is one way - if you need two way then youre talking about encryption which is different.
Normally for hashing youd do something like the following, though id urge not to take this verbatim but to research some on your own so oyu understand what youre doing and the concepts involved:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$account = 'someuser';
$password = 'passw0rd';
// your salt can be a constant that you never change, or can be user specific
// if you make it user specific then you need to store it as well as the password
$salt = "1j0i90#$t%";
$hash = hash('sha256', $password . $salt);
$acct = $xml->createElement($account);
$pw = $xml->createElement('password', $salt);
$acct->appendChild($pw);
$xml->appendChild($acct);
file_put_contents("pwDB.xml",$xml->saveXML());
And then to compare credentials like for a login you would do:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$account = 'someuser';
$password = 'passw0rd';
$salt = "1j0i90#$t%";
$hash = hash('sha256', $password . $salt);
$xpath = new DOMXPath($xml);
// look up by account name - assuming these are unique
$accountNodes = $xpath->query('//'.$account);
if($accountNodes->length) {
$accountNode = $accountNodes->item(0);
$pwNodes = $xpath->query('//password', $accountNode);
if($pwNodes->length) {
$pwNode = $pwNodes->item(0);
if($hash === (string) $pwNode) {
// authentication OK!
}
}
}
$title = '228-example-of-the-title'
I need to convert the string to:
Example Of The Title
How would I do that?
A one-liner,
$title = '228-example-of-the-title';
ucwords(implode(' ', array_slice(explode('-', $title), 1)));
This splits the string on dashes (explode(token, input)),
minus the first element (array_slice(array, offset))
joins the resulting set back up with spaces (implode(glue, array)),
and finally capitalises each word (thanks salathe).
$title = '228-example-of-the-title'
$start_pos = strpos($title, '-');
$friendly_title = str_replace('-', ' ', substr($title, $start_pos + 1));
You can do this using the following code
$title = '228-example-of-the-title';
$parts = explode('-',$title);
array_shift($parts);
$title = implode(' ',$parts);
functions used: explode implode and array_shift
$pieces = explode("-", $title);
$result = "";
for ($i = 1; $i < count(pieces); $i++) {
$result = $result . ucFirst($pieces[$i]);
}
$toArray = explode("-",$title);
$cleanArray = array_shift($toArray);
$finalString = implode(' ' , $cleanArray);
// echo ucwords($finalStirng);
Use explode() to split the "-" and put the string in an array
$title_array = explode("-",$title);
$new_string = "";
for($i=1; $i<count($title_array); $i++)
{
$new_string .= $title_array[$i]." ";
}
echo $new_string;
i have an array contain value like :
Check_Value ('level_codes', '1000', '1001','(1000,1002,1004)', 'DO ', '1')==1
i want to get values and store into an other parametrs like :
$codes = level_codes;
$first_value = 1000;
$second_value = 1001;
$list_values = (1000,1002,1004);
$action = DO;
$timer = 1;
$case = ==;
$status = 1;
is there any one please help me to do this work...!
Use the list() function: http://php.net/list
If the order is always the same, the following will reassign the values to the variables you want:
$parts = preg_split("/'?\(|\)'?/", $original_values);
$sub1 = explode(',', $parts[1]);
$codes = trim(str_replace("'", '', $sub1[0])) ;
$first_value = trim(str_replace("'", '', $sub1[1]));
$second_value = trim(str_replace("'", '', $sub1[2]));
$list_values = "($parts[2])";
$sub2 = explode(',', $parts[3]);
$action = trim(str_replace("'", '', $sub2[1]));
$timer = trim(str_replace("'", '', $sub2[2]));
preg_match('/(\D+)(\d+)/', $parts[4], $matches);
$case = $matches[1];
$status = $matches[2];
There might be a more elegant way to do it, but the original value is a string, rather than an array.
If the array is string, i think you should look into preg_match
i have string like this
$string = 'aaaaaa, bbbbbb, cccccc, ';
and i want to modified it to be like this
$string = 'aaaaaa, bbbbbb, cccccc';
the last ',' and space is removed.
how to do this in php?
what is the function needed the achieve that?
my full code is like this
if(isset($_POST['accomodation'])) $accomodation = 'Accomodation, ';
if(isset($_POST['dance'])) $dance = 'Dance Lessons, ';
if(isset($_POST['vacation'])) $vacation = 'Vacation planning, ';
if(isset($_POST['group'])) $group = 'Group Vacation, ';
if(isset($_POST['inprivate'])) $inprivate = 'Private Vacation, ';
if(isset($_POST['land'])) $land = 'Land purchase/lease';
if(isset($_POST['all'])) $all = 'All';
#$interest = $accomodation.$dance.$vacation.$group.$inprivate.$land;
#echo $string;
*sorry for such dumb question, it's been so long i didn't touch native PHP programming
rtrim() function:
rtrim($string,', ');
but how are you defining the string? It may be that you can build it without the comma and space.
EDIT
$interests = array();
if(isset($_POST['accomodation'])) $interests[] = 'Accomodation';
if(isset($_POST['dance'])) $interests[] = 'Dance Lessons';
if(isset($_POST['vacation'])) $interests[] = 'Vacation planning';
if(isset($_POST['group'])) $interests[] = 'Group Vacation';
if(isset($_POST['inprivate'])) $interests[] = 'Private Vacation';
if(isset($_POST['land'])) $interests[] = 'Land purchase/lease';
if(isset($_POST['all'])) $all = 'All';
$interest = implode(', ',$interests);
echo $interest;
$string = preg_replace('/\s*,\s*$/', '', $string);
or, way cooler:
$string = rtrim($string, " ,");
Note that it does not matter the order of the characters in the pattern string.
#You last update.
This changes some things. You could put all your variables in one array and then implode it. Like so:
$items = array();
$items[] = $accomodation = 'Accomodation';
$items[] = $dance = 'Accomodation';
...
$result = implode(', ', $items)
$string = preg_replace( "/,\s*$/","",$string);
Should do the trick
Is it always a comma then a space at the end?
substr($string, 0, -2)
Often times, you can avoid the trailing comma altogether by changing the way you build the string. For example:
$count = 0;
foreach ($this as $that) {
if ($count != 0) {
$string .= ',';
}
$string .= $that['stuff'];
$count++;
}
Would remove the possibility of any trailing comma at the end, no matter the combination of results.