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!
Dada44Asked:
Who is Participating?
 
kebabsConnect With a Mentor Commented:
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
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.