Extracting data with a certain pattern from a .txt file [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 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.

Related

Content of File PHP [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 1 year ago.
Improve this question
I have a file that i opened:
$linie = file("hasla-kopia.txt"); (It's in Polish)
And i have a problem with getting content of a file where every element is in the next line.
This is how my file looks like:
enter image description here
I want the exact output on my php file. I know you can do it by nl2br() function but it's really important for me to make every element in a different HTML tag because i want my final output to be:
1000111011111 in decimal system is: some number
and I don't know how to do the content on separate lines, change to int and write this text after it. Can someone please help me out?
$lines = file('hasla-kopia.txt');
foreach ($lines as $line_num => $line) {
echo $line."<br>";
}
that allows reading line by line and each line is the variable $line
you can use
$file = file_get_contents("hasla-kopia.txt");
$em = explode("\n" , $file);
for($i=0;$i<count($em);$i++){
echo $em[$i];
};

PHP - Trying to delete some elements of an array then repost all the content to a 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 5 years ago.
Improve this question
if(isset($_POST["delete"])){
$file_name = 'ids';
touch($file_name);
$handle = fopen($file_name, 'r+');
$stream = file_get_contents('ids');
$pieces = explode(" ", $stream);
unset($pieces[0]);
unset($pieces[1]);
unset($pieces[2]);
file_put_contents("ids", "\n" );
file_put_contents("ids", $pieces);
fclose($handle);
}
So I'm sure this is wicked messy and a terrible way to accomplish my goal but I am learning PHP for school and this is giving me a lot of issues.
When I press my button I want to take the content I have in 'ids', change it by deleting the strings located in the array at 0,1,2 and then rewrite the new content to 'ids'. Any help would be appreciated.
I'm not sure where you are getting stuck, but I would guess it is regarding the "re-assembly" process, so I'll fix that.
Untested Code:
if(isset($_POST["delete"])){
$file_name='ids';
touch($file_name);
$handle=fopen($file_name, 'r+');
$stream=file_get_contents($file_name);
$content=str_replace(" ","\n",explode(" ", $stream,4)[3]); // explode into a max of 4 elements, access the last element, replace spaces with `\n`
file_put_contents($file_name,$content);
fclose($handle);
}

count amount of numbers in a string [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 5 years ago.
Improve this question
I have a array of random generated strings looking like this:
63njpn5u
byrtg1za
ht6wnz39
em1yyrju
2ytoxfxl
n5kaho14
zg92pg4n
gr9e7i01
u3t07ai4
I need a php code that will cycle trough these and output the ones back that contain one number. So in the example above I would've gotten back:
2ytoxfxl
em1yyrju
byrtg1za
I've tried using preg_match_all but I cant figure out how to use this with a array.
What you need is preg_grep:
print_r(preg_grep('/^\D*\d\D*$/', $your_array));
You can count like using string method :
$string = "2ytoxfx3l";
echo $int = strlen(intval(preg_replace('/[^0-9]+/', '', $string), 10));
Group of element:
$string = array( "63njpn5u","byrtg1za","ht6wnz39");
foreach($string as $str){
echo strlen(intval(preg_replace('/[^0-9]+/', '', $str), 10));
}

PHP preg_match to retrieve text? [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 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.

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.

Categories