0

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("\r\n", $_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 "\n"; 
?>
</textarea>
            <?php
        }
        else {

        }
    }
}
?>
codsane
  • 68
  • 9
  • what's the expected output? – Karoly Horvath Apr 20 '14 at 14:13
  • Don't you just need to move the ` – andrewsi Apr 20 '14 at 14:13
  • The code is supposed to output all available usernames into ONE textarea, but the code is looping and creating multiple. I have tried to move the tags outside, but failed in doing so. If you have an example of moving the tags it would be greatly appreciated, as maybe I did it wrong. – codsane Apr 20 '14 at 14:16
  • if you want only ONE, create only ONE... post the code where you actually tried it. – Karoly Horvath Apr 20 '14 at 14:18

2 Answers2

1

If you want one text area tag and loop of content, then you need to put your for loop inside the text area.

<textarea><? foreach($convert as $i): ?><?= $i."\r\n" ?><? endforeach ?></textarea>
jschavey
  • 412
  • 3
  • 15
1

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("\n", $usernames) . '</textarea>';
jonnu
  • 728
  • 4
  • 12
  • Try to use cURL to get a URL's http status. [Easy way to test a URL for 404 in PHP?](http://stackoverflow.com/a/408416/685326) – josephting Apr 20 '14 at 14:29