douxian7808 2014-04-20 14:11
浏览 14
已采纳

PHP:文本区内的回声?

currently I am working on a username checker. I am trying to get all the available usernames to echo into a textarea, the only problem is that because the textarea is within a function and an 'if' statement, instead of the code displaying all available usernames into 1 single text area, because the textarea is inside the function and 'if' statement, the textarea continues to produce with every username checked.

Live example of the problem: http://hawkgen.com/ogpost/

Code: (second textarea is the one producing the problem)

<?php

function getTitle($Url){
    $str = file_get_contents($Url);
    if(strlen($str)>0){
    preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
    return $title[1];
    }
    else
    return '404';
}
?>
<form action="index.php" method="post">
<textarea name="notes" value="username" rows="4" cols="50">
test
test1
test2
test3
test4
test5
</textarea>
<input type="submit" value="Submit">
</form>
<?php
$convert = explode("
", $_POST["notes"]);

for ($i=0;$i<count($convert);$i++) 
{
    if (strlen($convert[$i])>0) {
        $resultCheck = getTitle("http://www.youtube.com/" . $convert[$i]);
        if (strpos($resultCheck,'404') !== false) {
?>
<textarea id="myText" rows="10" cols="40">
<?php 
echo $convert[$i]; 
echo "
"; 
?>
</textarea>
            <?php
        }
        else {

        }
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dpgua04022 2014-04-20 14:18
    关注

    As andrewsi mentioned in the comments of your post, you can just aggregate the usernames in a loop and then display them together outside of the loop, like so:

    $usernames = array();
    for ($i=0;$i<count($convert);$i++) 
    {
        if (strlen($convert[$i])>0) {
            $resultCheck = getTitle("http://www.youtube.com/" . $convert[$i]);
            if (strpos($resultCheck,'404') !== false) {
                $usernames[] = $convert[$i];
            }
        }
    }
    
    echo '<textarea id="myText" rows="10" cols="40">' . implode("
    ", $usernames) . '</textarea>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部