简体   繁体   中英

Feeding a list of URLs from an HTML form to PHP array

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.

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

或者使用filter_var() + FILTER_VALIDATE_URL传递自定义回调以进行更严格的检查

如果您只是每行输入一个,可以按换行分割textarea字符串,请参阅https://stackoverflow.com/a/1483501/2213444

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM