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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.