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('')));

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!
Who is Participating?

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

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.

This finds http://anything.doc and stores a list in temp.html with each URL, one per line:


Source document:
<a href="">test1</a><a href="">test2</a>

$contents = file_get_contents('');
preg_match_all('~https?://.+\.doc~iU', $contents, $matches);
file_put_contents('temp.html', join("\n", $matches));

Open in new window

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:
$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

Open in new window


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:
So I was following your first post.

Also I need
stored in a variable not in temp.html
Anyway temp.html comes out empty ..

Thanks in advance!!
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.

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.
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 ( )
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:
echo file_get_contents('temp.html');

Open in new window

Dada44Author Commented:
Thanks pal, it was a typo bothering ...
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

From novice to tech pro — start learning today.