pincopallo_it Posted June 13, 2008 Share Posted June 13, 2008 Probably an easy question for those who know the answer but im getting crazy to understand why a substring does not work . I have this array called $new_array I cycle on it and when I try to get a substring I get no result ! Has it something to do with the fact $p in this case is not a string ? Can you help ? Thanks That is the code: $lungh_array= count($new_array); for ($contatore=0; $contatore < $lungh_array; $contatore++) { if (strpos($new_array[$contatore], ':') !== false) { $cont = is_odd($contatore); if($cont) { // $prova = $new_array[$contatore]; // $clean = trim($prova); // $a = substr($clean,0,5); // $p = $new_array[$contatore]; // $a = substr($p,0,5); echo "Time IN " . $new_array[$contatore]; } else echo "Time OUT " . $new_array[$contatore]; } } Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/ Share on other sites More sharing options...
zenag Posted June 13, 2008 Share Posted June 13, 2008 can u tell me what is the value u hve gvn for $new_array Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564672 Share on other sites More sharing options...
pincopallo_it Posted June 13, 2008 Author Share Posted June 13, 2008 16:14 Casal Boccone That is the value of an element and I need to extract the time. Thanks Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564676 Share on other sites More sharing options...
zenag Posted June 13, 2008 Share Posted June 13, 2008 u need to display "Time IN 16:30" right... Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564682 Share on other sites More sharing options...
zenag Posted June 13, 2008 Share Posted June 13, 2008 i ve tried something like the below it works for me & it displays TIME IN: 16:14 $cont = $contatore%2; if($cont) { $prova = $new_array[$contatore]; $clean = trim($prova); $a = substr($clean,0,5); $p = $new_array[$contatore]; $a = substr($p,0,5); echo "Time IN " . $a; } else{ Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564683 Share on other sites More sharing options...
pincopallo_it Posted June 13, 2008 Author Share Posted June 13, 2008 Does not work ! The mistery is getting worse ! i give you all the code and the file i should read ... please help ! <?php //?heck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "txt") && ($_FILES["uploaded_file"]["size"] < 350000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { echo "It's done! The file has been saved as: ".$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Errore: Only .txt files under 350Kb are accepted for upload"; } } else { echo "Error: No file uploaded"; } $file = $newname; //change this to point to the file. $fh = fopen($file, "r"); $code = fread($fh, filesize($file)); $code_array = explode("\n", $code); foreach ($code_array as $value){ if (strstr($value, "font face=arial size=2")){ $new_array[] = $value; } } // print_r($new_array); $lungh_array= count($new_array); for ($contatore=0; $contatore < $lungh_array; $contatore++) { if (strpos($new_array[$contatore], ':') !== false) { // echo "$new_array[$contatore]<BR>"; $cont = is_odd($contatore); // if cont is odd if($cont) { $prova = $new_array[$contatore]; $clean = trim($prova); $a = substr($clean,0,5); // $p = $new_array[$contatore]; // $a = substr($p,0,5); echo "Time OUT " . $new_array[$contatore]; } else { echo "Time IN " . $new_array[$contatore]; } } } fclose($file); function is_odd($number) { return $number & 1; // 0 = even, 1 = odd } ?> Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564689 Share on other sites More sharing options...
kenrbnsn Posted June 13, 2008 Share Posted June 13, 2008 Do all the strings you're trying to parse contain the time followed by a space? If so you can explode the string on that space and get the time string that way. <?php $str = '16:14 Casal Boccone'; list($time,$dmy) = explode(' ',$str,2); echo str; ?> Please remember to surround all code you post with tags. Ken Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564693 Share on other sites More sharing options...
pincopallo_it Posted June 13, 2008 Author Share Posted June 13, 2008 Now it s becoming a real mistery . I tried this code and I got: Time OUT xx <CODE> list($time,$dmy) = explode(' ',$new_array[$contatore],2); echo "Time OUT " . $time . "xx"; </CODE> if I try <CODE> list($time,$dmy) = explode(' ',$new_array[$contatore],2); echo "Time OUT " . $time . "xx"; </CODE> I got Time OUT .... but $new_array[$contatore] is always filled. Another idea ? Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564719 Share on other sites More sharing options...
zenag Posted June 13, 2008 Share Posted June 13, 2008 <?php //?heck that we have a file if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { //Check if the file is JPEG image and it's size is less than 350Kb $filename = basename($_FILES['uploaded_file']['name']); $ext = substr($filename, strrpos($filename, '.') + 1); if (($ext == "txt") && ($_FILES["uploaded_file"]["size"] < 350000)) { //Determine the path to which we want to save this file $newname = dirname(__FILE__).'/upload/'.$filename; //Check if the file with the same name is already exists on the server if (!file_exists($newname)) { //Attempt to move the uploaded file to it's new place if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { echo "It's done! The file has been saved as: ".$newname; } else { echo "Error: A problem occurred during file upload!"; } } else { echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; } } else { echo "Errore: Only .txt files under 350Kb are accepted for upload"; } } else { //echo "Error: No file uploaded"; } $file = 'files.txt'; //change this to point to the file. $fh = fopen($file, "r"); $code = fread($fh, filesize($file)); $code_array = explode("\n", $code); foreach ($code_array as $value){ if (strstr($value, "font face=arial size=2")){ $new_array[] = $value; } } // print_r($new_array); $lungh_array= count($new_array); for ($contatore=0; $contatore < $lungh_array; $contatore++) { if (strpos($new_array[$contatore], ':') !== false) { $p = $new_array[$contatore]."<br>"; $q = strlen($p); //echo $p."<br>"; $a = substr($p,24,5); // echo "$new_array[$contatore]<BR>"; // $cont = is_odd($contatore); // if cont is odd $cont = $a%2; //echo $new_array[$contatore]; if($cont==1) { echo "Time IN:".$a."<br>"; $prova = $new_array[$contatore]; $clean = trim($prova); $a = substr($clean,0,5); // $p = $new_array[$contatore]; // $a = substr($p,0,5); //echo "Time OUT " . $new_array[$contatore]; //$aa = substr($new_array[$contatore],0,5); //echo "Time OUT " .$a; } else { echo "Time OUT " . $a."<br>"; } } } fclose(); function is_odd($number) { return $number & 1; // 0 = even, 1 = odd } ?> Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564722 Share on other sites More sharing options...
pincopallo_it Posted June 13, 2008 Author Share Posted June 13, 2008 Thanks Zemag I did not know what i did wrong but your code works ! Many thanks Link to comment https://forums.phpfreaks.com/topic/110043-substring-does-not-work-sigh/#findComment-564735 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.