note 107921 deleted from function.explode by googleguy
| From: | googleguy@php.net | Date: | Thu, 22 Nov 2012 16:50:24 +0000 |
| Subject: | note 107921 deleted from function.explode by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192088@lists.php.net to get a copy of this message | ||
Note Submitter: Mobin Hosseini
----
Hello,
I just wanted to note some behaviours of the explode function, well i am submitting a bunch of urls,
one each line and i am using the "\n" (it should be within double quotes) as the delimiter
and i am iterating to check for dupe values and always the last value stays unchecked unless i am
appending a new line "\n" to the $_POST.
Let me demonstrate what i mean(sorry if there is any syntax error i am just writing this
on-the-fly):
if(isset($_POST['files']) && !empty($_POST['files'])){
$regex = "((http|ftp)+\:\/\/(www.)?(.*)/(.*)(\.)?(.*)?)";
$ls = $_POST['files'];
$posts = explode("\n", $ls);
//remove duplicates
$count = count($posts);
for($i=0;$i<$count;$i++){
if($posts[$i]=="") continue;
for($j=$i+1;$j<$count;$j++){
if($posts[$j]=="") continue;
if((string)$posts[$i] == (string)$posts[$j]){
$posts[$j] = "";
}
}
}
$i=0;
$files = array();
foreach($posts as $f ){
if(preg_match($regex,$f,$matches)){
$files[] = $f;
$i++;
}
if($i>=10){
break;
}
}
echo "<pre>";
print_r($files);
echo "</pre>";
}
submit some urls in this format :
http://www.google.com/admin.db
http://www.google.com/admin.db[dont hit the
"enter" key here]
i didn't have much time to look at it in deep so i just appended using javascript the new line
:
document.getElementById('files').value += "\n";
This way the last dupe value will get deleted too otherwise it wont!