0

I have a .php file which I will be using to submit requests to a certain API. This API will return information regarding certain domain URLs, such as the domains age, PageRank, etc.

The part of the PHP file which is responsible for feeding the API call URL with the domain names I'm interested in looks as follows:

$batchedDomains = array('www.example.com', 'www.cnn.com', 'www.apple.com'); 

What I would like to do is feed this array information through a very simple HTML form. My current HTML for the form looks as follows:

<form name="myform" action="apitest.php" method="POST">
    <input type="hidden" name="check_submit" value="1" />

    URL List:<br /> 
    <textarea name="urls" rows="20" cols="60">Enter URLs</textarea><br />
    <input type="submit" />
</form>

Here is what I would like to see happen: whenever I enter a list of domain URLs into the HTML form (one domain per line), I would like the $batchedDomains array to be populated with those values.

Can anyone help me out with this? Or if you have a suggestion for a different solution I'm of course willing to hear it out.

I do not want this information printed anywhere, as it will simply be used by the php script to call the API and display the results.

Thank you.

3 Answers 3

2
$urls = array_filter(explode(PHP_EOL, $_POST['urls']), 'parse_url');

Or pass a custom callback with filter_var() + FILTER_VALIDATE_URL for stricter checks

4
  • did you mean array_map('parseurl', array_filter(...));? May 16, 2013 at 14:08
  • @billyonecan this solution works as expected... codepad.viper-7.com/DhRerI
    – brbcoding
    May 16, 2013 at 14:12
  • @billyonecan: array_map would replace the item with the return value of parse_url. OP doesn't seem to want that. Actually he didn't ask for validation either :)
    – nice ass
    May 16, 2013 at 14:13
  • Ahh I see. The only thing with using that as a means of filtering is that it will very rarely return false (returns empty input, simple strings etc.) +1 for the use of PHP_EOL :) May 16, 2013 at 14:30
0

If you're just entering one per line, you can split the textarea string by newlines, see https://stackoverflow.com/a/1483501/2213444.

0
<?php
// has no error checking, you'll want to check that $_POST['urls'] exists
$textarea = explode("\r\n", $_POST['urls']);
print_r($textarea);

?>
    <form action="" method="post">
        <textarea name='urls' rows='20' cols='60'>
        </textarea>
        <input type="submit">
    </form>

Working Example

3
  • Hi! why is the Print_r required here? since I do not want to print anything anywhere. I just want to populate the $batchedDomains array in my php file. Perhaps I'm misunderstanding something though! May 16, 2013 at 14:10
  • It's not required... It's just there to show you that the variable was populated with your urls.
    – brbcoding
    May 16, 2013 at 14:11
  • Either way, thanks a lot - the first line explode("\r\n", $_POST['urls']); is enough and it's exactly what I wanted :) Thank you! May 16, 2013 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.