i have registry data in text as below:
/Classes/CLSID/AppID,SZ,{0010890e-8789-413c-adbc-48f5b511b3af},
/Classes/CLSID/InProcServer32,KEY,,2011-10-14 00:00:33
/Classes/CLSID/InProcServer32/,EXPAND_SZ,%SystemRoot%\x5Csystem32\x5CSHELL32.dll,
/Classes/CLSID/InProcServer32/ThreadingModel,SZ,Apartment,
/Classes/CLSID/,KEY,,2011-10-14 00:00:36
/Classes/CLSID/,SZ,,
/Classes/CLSID/InprocServer32,KEY,,2011-10-14 00:00:36
/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll,
then i do $registry = explode "\n" and create list of arrays below:
var_dump($registry);
[1]=> string(121) "/Classes/CLSID/AppID,SZ,{0010890e-8789-413c-adbc-48f5b511b3af},"
[2]=> string(139) "/Classes/CLSID/InProcServer32,KEY,,2011-10-14 00:00:33"
[3]=> string(89) "/Classes/CLSID/InProcServer32/,EXPAND_SZ,%SystemRoot%\x5Csystem32\x5CSHELL32.dll,"
[4]=> string(103) "/Classes/CLSID/InProcServer32/ThreadingModel,SZ,Apartment,"
[5]=> string(103) "/Classes/CLSID/,KEY,,2011-10-14 00:00:36"
[6]=> string(121) "/Classes/CLSID/,SZ,,"
[7]=> string(139) "/Classes/CLSID/InprocServer32,KEY,,2011-10-14 00:00:36"
[8]=> string(89) "/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll,"
i also have keywords in array form
var_dump($keywords);
[1]=> string(12) "Math.dll"
[2]=> string(12) "System.dll"
[3]=> string(12) "inetc.dll"
[4]=> string(12) "time.dll"
i want to show lines in $registry that consist string in $keywords, so i create 1 function below:
function separate($line) {
global $keywords;
foreach ($keywords as $data_filter) {
if (strpos($line, $data_filter) !== false) {
return true;
}
}
return false;
}
$separate = array_filter($registry, 'separate');
since in $keywords consists "time.dll" so the codes produce result as below:
var_dump($seperate);
[1]=> string(89) "/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll,"
in my case the result is not true because, mstime.dll != time.dll and the information is improper.
the output should be empty.
lets say i replace the "\x5C" as space, there is any function that can do the job? thank you in advance.
There's preg_match.
To go along with the array_filter way you have to do things:
function separate($line) {
global $keywords;
foreach ($keywords as $data_filter) {
// '.' means any character in regex, while '\.' means literal period
$data_filter = str_replace('.', '\.', $data_filter);
if (preg_match("/\\x5C{$data_filter}/", $line)) {
return true;
}
}
return false;
}
This would return false for
/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Cmstime.dll,
but true for
/Classes/CLSID/InprocServer32/,C:\x5CWINDOWS\x5Csystem32\x5Ctime.dll,
If you're not familiar with Regular Expressions, they are awesome and powerful. You can customize mine as needed to suit your situation.
Related
I'm trying to unset a couple items from an array at once, send unsetted items to another array.
array(6) {
[0]=> string(65) "https://www.kintetsu-re.co.jp/mansion_kansai/outline/midosuji241/"
[1]=> string(41) "https://geo.8984.jp/outline/suminodo.html"
[2]=> string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0141"
[3]=> string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0136"
[4]=> string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0099"
[5]=> string(53) "https://www.sgr-sumai.jp/mansion/tezukayama21/outline"
}
Three links in this array starts with http://www.sohgoh-outline.jp . So I'm trying to unset those. But not one by one. I can already doing it. For example, I tried to locate those with strpos
$needle = "http://www.sohgoh-outline.jp/";
foreach ($link as $unset){
if (($index = strpos($unset, $needle)) !== false){
$renai [] = $unset[$index];
unset($unset[$index]);
}
}
But this error popping up.
Cannot unset string offsets
Any suggestions?
change your code like this:
foreach ($link as $k => $unset){
if ((strpos($unset, $needle)) !== false)
{
$renai [] = $link[$k]; // This will add the value to new array.
unset($link[$k]); // THIS WILL UNSET THE VALUE.
}
}
The simplest method is probably regex with preg_grep and array_diff.
$out = preg_grep("/.*?(sohgoh-outline\.jp).*/", $arr);
var_dump($out); //sohgoh-outline.jp links
$arr = array_diff($arr, $out);
var_dump($arr); // all but sohgoh-outline.jp links
Output of above code:
array(3) {
[2]=>
string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0141"
[3]=>
string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0136"
[4]=>
string(56) "http://www.sohgoh-outline.jp/index.php?bunjo_number=0099"
}
array(3) {
[0]=>
string(65) "https://www.kintetsu-re.co.jp/mansion_kansai/outline/midosuji241/"
[1]=>
string(41) "https://geo.8984.jp/outline/suminodo.html"
[5]=>
string(53) "https://www.sgr-sumai.jp/mansion/tezukayama21/outline"
}
https://3v4l.org/Um46H
I have got an array wich contains several strings like this:
array(133) {
[0]=>
array(1) {
["row"]=>
array(5) {
[0]=>
string(10) "testd ' /% ata"
[1]=>
string(14) "testdata 111"
[2]=>
string(17) "testdata 123"
[3]=>
string(0) ""
[4]=>
string(0) ""
}
}
[1]=>
array(1) {
["row"]=>
array(5) {
[0]=>
string(9) "198"
[1]=>
string(14) "testdata"
[2]=>
string(41) "testdat"
[3]=>
string(0) ""
[4]=>
string(0) ""
}
}
My question is how can I check the strings within the array on special chars? These special chars are causing a syntax error when importing into my DB.
I think i need to use something like this?
preg_replace('/[^a-zA-Z0-9_ -%][().][\/]/s', '', $String);
Can anyone help me on this one?
Allright nog i have created this piece of code:
// search for special chars in the import data and remove them
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
foreach ($data_set as $data)
foreach ($data_set['data'] as $row) {
if(strpbrk($row, $illegal)) {
echo($row);
die();
}
else {
//not allowed ,escape or do what you want
echo("no characters found");
die();
}
var_dump($row);
die();
}
But this still gives an error:
A PHP Error was encountered
Severity: Warning
Message: strpbrk() expects parameter 1 to be string, array given
Filename: controllers/import.php
Line Number: 153
no characters found
You may have a look at strpbrk. It should solve your problem.
Example:
$tests = "testd ' /% ata";
);
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Contains special chars";
But i suggest to escape your strings before inserting to database,it's much safer.
foreach ($yourArray as $array)
foreach ($array['row'] as $row) {
if(strpbrk($row, $illegal)) {
//allowed ,insert to db
}
else {
//not allowed ,escape or do what you want
}
}
Given:
$val = "font-size:12px;color:#ff0000;font-family:Arial";
The following code will explode the string twice, to produce an array of arrays:
$val = explode(';',$val);
foreach($val as &$v)
$v = explode(':',$v);
var_dump($val);
The output is:
array(3) {
[0]=>
array(2) {
[0]=>
string(9) "font-size"
[1]=>
string(4) "12px"
}
[1]=>
array(2) {
[0]=>
string(4) "fill"
[1]=>
string(7) "#ff0000"
}
[2]=>
&array(2) {
[0]=>
string(11) "font-family"
[1]=>
string(5) "Arial"
}
}
Is there a more efficient / cleaner way to achieve the same result?
I'd prefer something with no lambda functions since PHP 5.2 doesn't support them. But this is a purely intellectual question anyway, so, that's just a preference.
You can try with:
$input = "font-size:12px;color:#ff0000;font-family:Arial";
preg_match_all('/([^:]*?):([^;]*);?/', $input, $matches);
$output = array_combine($matches[1], $matches[2]);
Output:
array(3) {
["font-size"]=>
string(4) "12px"
["color"]=>
string(7) "#ff0000"
["font-family"]=>
string(5) "Arial"
}
I'd recommend against references--you can run into some odd errors. But your approach is fine. Alternatively, you could do something with array_map:
$val = array_map(function($v) { return explode(':', $v); }, explode(';', $val)));
I'm planning to build web app and i heve question how to create mentions for name "#myname" like facebook or twitter
Find them in a string with this regex....
$str = 'Yo #bob, what\'s up? I have a new email, tom#bob.com, tell #john too, from #alex';
preg_match_all('/\s#(?P<mention>\w+?)\b/', $str, $mentions);
var_dump($mentions);
Output
array(3) {
[0]=>
array(3) {
[0]=>
string(5) " #bob"
[1]=>
string(6) " #john"
[2]=>
string(6) " #alex"
}
["mention"]=>
array(3) {
[0]=>
string(3) "bob"
[1]=>
string(4) "john"
[2]=>
string(4) "alex"
}
[1]=>
array(3) {
[0]=>
string(3) "bob"
[1]=>
string(4) "john"
[2]=>
string(4) "alex"
}
}
Of course, you could real time detect them in a string in JavaScript, just change that regex to a JavaScript one.
Then, you would look up your database based on the tagged name, and then do what you need to do!
You could cut down on requests by limiting your regex to what makes a valid username, e.g. /\w{6,}/.
function mention($txt)
{
$txt = ' '.$txt;
preg_match_all('/\s#(?P<mention>\w+?)\b/', $txt, $mentions);
echo "<pre>";
print_r($mentions);
if (isset($mentions[0])) {
foreach ($mentions[0] as $key => $value) {
$mention = strtolower(str_replace(array("#"," "), "", $value));
$txt = str_replace($value, ' '.$mention.'', $txt);
}
}
return trim($txt);
}
Could you trim all $_POST vars? because i have a very long list right now for trim each var. looks very unprofessional. i thought trim($_POST); would maybe work but it didnt :]
you can do this with array_map:
$_POST = array_map('trim', $_POST);
Works with multi-dimensional arrays
array_walk_recursive($_POST, function (&$val)
{
$val = trim($val);
});
foreach($_POST as &$p) $p = trim($p);
Quick and simple:
foreach($_POST as $key => $val)
{
$_POST[$key] = trim($val);
}
The simplest, and cleanest (in my opinion), is to use the built in array_map function:
array_map('trim', $_POST);
You can also apply a method of your own by passing an array as the first callback-parameter like so:
array_map(array('My_Class', 'staticMethod'), $_POST); // Invoke a static method
array_map(array($myObject, 'objectMethod'), $_POST);
// Invoke $myObject->objectMethod for each element of $_POST
Update based on a comment below
Sometimes the $_POST array may contain arrays. If you want to trim contents of those arrays as well, there are many custom implementations of array_map_recursive available in the PHP manual user notes. Go there and choose one for yourself. If you don't like to take a custom implementation, array_walk_recursive is also a good option for you.
You can do this with array_walk().
Using recursive function you can do that.
PHP
// Static $_POST Array.
$_POST['1']='one ';
$_POST['2']=' two';
$_POST['3'][]=' three ';
$_POST['4'][][]=' four';
$_POST['5'][0][1][3]='five ';
// Recursive function for trim data.
function trim_recursive($array){
$return = array();
foreach($array as $key=>$values){
if(is_array($values)===true){
$return[$key] = trim_recursive($values);
}
else{
$return[$key] = trim($values);
}
}
return $return;
}
// Usage.
$_POST = trim_recursive($_POST);
Output
// Output before trim.
array(5) {
[1]=>
string(4) "one "
[2]=>
string(4) " two"
[3]=>
array(1) {
[0]=>
string(9) " three "
}
[4]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(5) " four"
}
}
[5]=>
array(1) {
[0]=>
array(1) {
[1]=>
array(1) {
[3]=>
string(5) "five "
}
}
}
}
// Output after trim.
array(5) {
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
array(1) {
[0]=>
string(5) "three"
}
[4]=>
array(1) {
[0]=>
array(1) {
[0]=>
string(4) "four"
}
}
[5]=>
array(1) {
[0]=>
array(1) {
[1]=>
array(1) {
[3]=>
string(4) "five"
}
}
}
}