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条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b