[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 873
  • Last Modified:

Find string in a file and save it as a variable

Hi all,

I have
file_put_contents('temp.html', preg_replace('//', '', file_get_contents('http://www.mypage.com/')));

I want to find in temp.html the strings that start with http and end with .doc and save them into a variable/array.

How can I do that??

Thanks a lot in advance!
0
Dada44
Asked:
Dada44
  • 4
  • 3
1 Solution
 
kebabsCommented:
This finds http://anything.doc and stores a list in temp.html with each URL, one per line:

e.g.

Source document:
<a href="http://example.com/test1.doc">test1</a><a href="http://example.com/test2.doc">test2</a>

Result:
http://example.com/test1.doc
http://example.com/test2.doc
<?php
 
$contents = file_get_contents('http://example.com');
 
preg_match_all('~https?://.+\.doc~iU', $contents, $matches);
 
file_put_contents('temp.html', join("\n", $matches));

Open in new window

0
 
kebabsCommented:
The last line of the code in my comment should actually be:
file_put_contents('temp.html', join("\n", $matches[0]));

Or maybe this is what you wanted:
<?php
 
$contents = file_get_contents('temp.html');
 
preg_match_all('~https?://.+\.doc~iU', $contents, $matches);
 
// $documents is an array of files
$documents = $matches[0];
 
// Output the list
print_r($documents);

Open in new window

0
 
Dada44Author Commented:
What I need is:
http://example.com/test1.doc
http://example.com/test2.doc
So I was following your first post.

Also I need
http://example.com/test1.doc
http://example.com/test2.doc
stored in a variable not in temp.html
Anyway temp.html comes out empty ..

Thanks in advance!!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kebabsCommented:
Did you make the edit mentioned in the second post to the code in the first post?

Anyway, I think you want what I had in the second post, that one stores each URL in an array.
0
 
Dada44Author Commented:
Yes I chnaged in the first post what you told me to change in the second and I get an empty file.
If I use the second post the output is:
Array ( )
0
 
kebabsCommented:
That's strange because I tested that code. I tested it again just then to make sure.

Are you sure there is something in temp.html to read from?

Can you run this to make sure that the contents to search in do in fact exist please:
<?php
 
echo file_get_contents('temp.html');

Open in new window

0
 
Dada44Author Commented:
Thanks pal, it was a typo bothering ...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now