Form output content of URL


Is there anyway of typing a URL into a form text box and then the output page echos the content of that URL in your page?

In the same way you can use file_get_contents.

Thank you

Yes, you can do just like this:



$url = $_POST[“url”];
echo file_get_contents($url);

Thanks PHP coder for such a quick response.

That works perfectly, however I have some preg_replace arrays after that piece of form output code.

At the moment it gets the content from the website but it ignores all of my replace functions.

Would it be a case of re-ordering things a bit?

Thank you

Amy, do you apply preg_replace to url received from html form, or to page content?

I’m not entirely sure I understand; my index page has a form with the URL field in, then the output displays the content of whichever URL is typed in. The preg_replace is applied to that output page.

Here is a shortened version of my code:

<?php $string = $_POST["url"]; echo file_get_contents($string); $patterns[0] = '/\bwoman\b/i'; $patterns[1] = '/\bman\b/i'; $patterns[2] = '/\bintwoman\b/i'; $patterns[3] = '/\bintman\b/i'; $replacements[0] = 'intwoman'; $replacements[1] = 'intman'; $replacements[2] = 'man'; $replacements[3] = 'woman'; echo preg_replace('$1', $patterns, $replacements, $string); ?>

Thank you

Amendment to last post:

Sorry, the code had a mistake, it should read:

<?php $string = $_POST["url"]; echo file_get_contents($string); $patterns[0] = '/\bwoman\b/i'; $patterns[1] = '/\bman\b/i'; $patterns[2] = '/\bintwoman\b/i'; $patterns[3] = '/\bintman\b/i'; $replacements[0] = 'intwoman'; $replacements[1] = 'intman'; $replacements[2] = 'man'; $replacements[3] = 'woman'; echo preg_replace($patterns, $replacements, $string); ?>

And the form code is as you specified:



Instead of echo’ing page content, you need to assign it to a $string valuable so that preg_replace apply to this content:
[php]$string = $_POST[“url”];
$string = file_get_contents($string);[/php]

I meant $string variable :slight_smile:

Ah, brilliant, thanks so much.

Works perfectly now!

I don’t suppose there is anyway of retaining the capitalisation on the words that are replaced? I’ve got it so the terms are not case sensitive by using the /i modifier.

Just thought i’d check if this was possible or not.

Thanks again

Sponsor our Newsletter | Privacy Policy | Terms of Service