Check and modify Capitalized letters in string - php

I'm trying to figure out how to make this code work.
I input some text via variable into code:
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
You will see that for the Genome there is a Nn and for the Fenome there is a Ee
Upon this happening I need it to give a 50/50 chance for a compared result to be EITHER Ee OR Nn - The code I have right now can only check them individually (so sometimes I ger Ee AND Nn, other times I will get ee and nn) and I feel there could be a much easier method of achieving this than what I'm trying:
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
if ($gepieces[0] === $fepieces[0]) {
$ss = $gepieces[0];
} else {
$ss = rand(1,2);
if ($ss == 1) {
$ss = $gepieces[0];
} else {
$ss = $fepieces[0];
}
}
if ($gepieces[1] === $fepieces[1]) {
$ee = $gepieces[1];
} else {
$ee = rand(1,2);
if ($ee == 1) {
$ee = $gepieces[1];
} else {
$ee = $fepieces[1];
}
}
if ($gepieces[2] === $fepieces[2]) {
$ff = $gepieces[2];
} else {
$ff = rand(1,2);
if ($ff == 1) {
$ff = $gepieces[2];
} else {
$ff = $fepieces[2];
}
}
if ($gepieces[3] === $fepieces[3]) {
$nn = $gepieces[3];
} else {
$nn = rand(1,2);
if ($nn == 1) {
$nn = $gepieces[3];
} else {
$nn = $fepieces[3];
}
}
if ($gepieces[4] === $fepieces[4]) {
$oo = $gepieces[4];
} else {
$oo = rand(1,2);
if ($oo == 1) {
$oo = $gepieces[4];
} else {
$oo = $fepieces[4];
}
}
echo $cubgender." - ".$ss."/".$ee."/".$ff."/".$nn."/".$oo."<br/>";
}

I'm a colossal idiot!
Figured out my own issue
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
if ($genome === $fenome) {
$cubgeno = $genome;
} else {
$cubgeno = rand(1,2);
if ($cubgeno == 1) {
$cubgeno = $genome;
} else {
$cubgeno = $fenome;
}
}
echo $cubgender." - ".$cubgeno."<br/>";

$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
$sequence = "";
for ($i = 0;$i < count($fepieces); $i++) {
rand(1,2) == 1 ? $sequence .= $gepieces[$i] : $sequence .= $fepieces[$i];
}
echo $sequence;

Related

Auto Description From Tags

I'm trying to generate auto description from tags.
The code was working but after updating my site to Laravel 6 in stop working. I need to get it back working.
if( !empty( $request->description ) )
{
$description = Helper::checkTextDb($request->description);
}
else
{
$a_key = explode(",", strtolower($request->tags));
if(count($a_key) == 0)
$description = 'This is a great thing';
else
{
$description_get_keys = '';
foreach ($a_key as &$value)
{
if($value == end($a_key) && count($a_key) != 1)
$description_get_keys = $description_get_keys.' and '.$value.'.';
else if(count($a_key) == 1)
$description_get_keys = $value.'.';
else if (count($a_key) > 1 && $a_key[0] == $value)
$description_get_keys = $value;
else
$description_get_keys = $description_get_keys.', '.$value;
}
$description = 'This is a great thing about '.$description_get_keys;
}
}
I see a couple things that could possibly be an issue, not knowing what came before this code.
I will assume that the $request variable is an instance of Illuminate\Http\Request and that it is available in the function, right?
Try this updated code:
if($request->has('description'))
{
$description = Helper::checkTextDb($request->description);
}
else if ($request->has('tags'))
{
if (strpos($request->tags, ',') === false)
{
$description = 'This is a great thing';
}
else {
$a_key = explode(",", strtolower($request->tags));
$a_count = count($a_key);
$description_get_keys = '';
for ($i = 0; $i < $a_count; $i++)
{
if ($a_count == 1) {
$description_get_keys = "{$a_key[$i]}.";
}
else {
// first
if ($i === 0) {
$description_get_keys = $a_key[0];
}
// last
else if ($i === $a_count - 1) {
$description_get_keys .= " and {$a_key[$i]}.";
}
// middle
else {
$description_get_keys .= ", {$a_key[$i]}";
}
}
}
$description = "This is a great thing about {$description_get_keys}";
}
}
I wrote that quick so hopefully there are no errors.

PHP Link Shortener

Zack here.. I've recently run into a spot of trouble with my URL Shortener.. Can you check the below code for errors? Thanks so much!
<?php
$db = new mysqli("localhost","pretzel_main","00WXE5fMWtaVd6Dd","pretzel");
function code($string_length = 3) {
$permissible_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$char_length = strlen($permissible_characters);
$random = '';
for ($i = 0; $i < $string_length; $i++) {
$random .= $permissible_characters[rand(0, $char_length - 1)];
}
return $random;
}
if (substr($_POST['long'], 0, 7) == http://) {
$url = $_POST['long'];
} elseif (substr($_POST['long'], 0, 8) == https://) {
$url = $_POST['long'];
} else {
$url = "http://".$_POST['long'];
}
$result = $db->prepare("INSERT INTO links VALUES('',?,?)");
$result->bind_param("ss",$url, $title);
$result->execute();
$title = $code();
?>
Good luck, and Thanks in advance!
- Zack
Try this :
<?php
$db = new mysqli("localhost","pretzel_main","00WXE5fMWtaVd6Dd","pretzel");
function code($string_length = 3) {
$permissible_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$char_length = strlen($permissible_characters);
$random = '';
for ($i = 0; $i < $string_length; $i++) {
$random .= $permissible_characters[rand(0, $char_length - 1)];
}
return $random;
}
if (substr($_POST['long'], 0, 7) == "http://") {
$url = $_POST['long'];
} elseif (substr($_POST['long'], 0, 8) == "https://") {
$url = $_POST['long'];
} else {
$url = "http://".$_POST['long'];
}
$result = $db->prepare("INSERT INTO links VALUES('ss','$url','$title')");
$result->execute();
$title = code(); // no need of $ sign here
?>

Is there a way to process PDF files as text files in PHP?

My company receives coupons codes from another company and I need to process them to use them in my company's website. The problem is that they send me a PDF file with the codes, and they say their system can only export them in PDF. Strange.
I've tried several times to let them know that I need those coupons in plain text separated by something (a.k.a CSV) and they doesn't take any notice of that.
My website is written in PHP, and uses MySQL. I would like to offer a way to upload that coupons file and add those to the database. It would be easy considering it is a CSV file but it's not.
Is there any way I can programatically -not manually- process PDF files as text files? or, any workaround for this situation?
I have found pdf2text to have worked well for quite some time.
Will not work with image (TIFF) PDF. will work with text searchable PDF.
include('/home/user/php/class.pdf2text.php');
$p2t = new PDF2Text();
$p2t ->setFilename($pdf);
$p2t ->decodePDF();
$data = $p2t ->output();
$pos = strpos($data,$search);
if (pos){...}
Source:
<?php
class PDF2Text {
// Some settings
var $multibyte = 4; // Use setUnicode(TRUE|FALSE)
var $convertquotes = ENT_QUOTES; // ENT_COMPAT (double-quotes), ENT_QUOTES (Both), ENT_NOQUOTES (None)
var $showprogress = true; // TRUE if you have problems with time-out
// Variables
var $filename = '';
var $decodedtext = '';
function setFilename($filename) {
// Reset
$this->decodedtext = '';
$this->filename = $filename;
}
function output($echo = false) {
if($echo) echo $this->decodedtext;
else return $this->decodedtext;
}
function setUnicode($input) {
// 4 for unicode. But 2 should work in most cases just fine
if($input == true) $this->multibyte = 4;
else $this->multibyte = 2;
}
function decodePDF() {
// Read the data from pdf file
$infile = #file_get_contents($this->filename, FILE_BINARY);
if (empty($infile))
return "";
// Get all text data.
$transformations = array();
$texts = array();
// Get the list of all objects.
preg_match_all("#obj[\n|\r](.*)endobj[\n|\r]#ismU", $infile . "endobj\r", $objects);
$objects = #$objects[1];
// Select objects with streams.
for ($i = 0; $i < count($objects); $i++) {
$currentObject = $objects[$i];
// Prevent time-out
#set_time_limit ();
if($this->showprogress) {
// echo ". ";
flush(); ob_flush();
}
// Check if an object includes data stream.
if (preg_match("#stream[\n|\r](.*)endstream[\n|\r]#ismU", $currentObject . "endstream\r", $stream )) {
$stream = ltrim($stream[1]);
// Check object parameters and look for text data.
$options = $this->getObjectOptions($currentObject);
if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])) )
// if ( $options["Image"] && $options["Subtype"] )
// if (!(empty($options["Length1"]) && empty($options["Subtype"])) )
continue;
// Hack, length doesnt always seem to be correct
unset($options["Length"]);
// So, we have text data. Decode it.
$data = $this->getDecodedStream($stream, $options);
if (strlen($data)) {
if (preg_match_all("#BT[\n|\r](.*)ET[\n|\r]#ismU", $data . "ET\r", $textContainers)) {
$textContainers = #$textContainers[1];
$this->getDirtyTexts($texts, $textContainers);
} else
$this->getCharTransformations($transformations, $data);
}
}
}
// Analyze text blocks taking into account character transformations and return results.
$this->decodedtext = $this->getTextUsingTransformations($texts, $transformations);
}
function decodeAsciiHex($input) {
$output = "";
$isOdd = true;
$isComment = false;
for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
$c = $input[$i];
if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}
switch($c) {
case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
case '%':
$isComment = true;
break;
default:
$code = hexdec($c);
if($code === 0 && $c != '0')
return "";
if($isOdd)
$codeHigh = $code;
else
$output .= chr($codeHigh * 16 + $code);
$isOdd = !$isOdd;
break;
}
}
if($input[$i] != '>')
return "";
if($isOdd)
$output .= chr($codeHigh * 16);
return $output;
}
function decodeAscii85($input) {
$output = "";
$isComment = false;
$ords = array();
for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
$c = $input[$i];
if($isComment) {
if ($c == '\r' || $c == '\n')
$isComment = false;
continue;
}
if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
continue;
if ($c == '%') {
$isComment = true;
continue;
}
if ($c == 'z' && $state === 0) {
$output .= str_repeat(chr(0), 4);
continue;
}
if ($c < '!' || $c > 'u')
return "";
$code = ord($input[$i]) & 0xff;
$ords[$state++] = $code - ord('!');
if ($state == 5) {
$state = 0;
for ($sum = 0, $j = 0; $j < 5; $j++)
$sum = $sum * 85 + $ords[$j];
for ($j = 3; $j >= 0; $j--)
$output .= chr($sum >> ($j * 8));
}
}
if ($state === 1)
return "";
elseif ($state > 1) {
for ($i = 0, $sum = 0; $i < $state; $i++)
$sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
for ($i = 0; $i < $state - 1; $i++) {
try {
if(false == ($o = chr($sum >> ((3 - $i) * 8)))) {
throw new Exception('Error');
}
$output .= $o;
} catch (Exception $e) { /*Dont do anything*/ }
}
}
return $output;
}
function decodeFlate($data) {
return #gzuncompress($data);
}
function getObjectOptions($object) {
$options = array();
if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
$options = explode("/", $options[1]);
#array_shift($options);
$o = array();
for ($j = 0; $j < #count($options); $j++) {
$options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
if (strpos($options[$j], " ") !== false) {
$parts = explode(" ", $options[$j]);
$o[$parts[0]] = $parts[1];
} else
$o[$options[$j]] = true;
}
$options = $o;
unset($o);
}
return $options;
}
function getDecodedStream($stream, $options) {
$data = "";
if (empty($options["Filter"]))
$data = $stream;
else {
$length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
$_stream = substr($stream, 0, $length);
foreach ($options as $key => $value) {
if ($key == "ASCIIHexDecode")
$_stream = $this->decodeAsciiHex($_stream);
elseif ($key == "ASCII85Decode")
$_stream = $this->decodeAscii85($_stream);
elseif ($key == "FlateDecode")
$_stream = $this->decodeFlate($_stream);
elseif ($key == "Crypt") { // TO DO
}
}
$data = $_stream;
}
return $data;
}
function getDirtyTexts(&$texts, $textContainers) {
for ($j = 0; $j < count($textContainers); $j++) {
if (preg_match_all("#\[(.*)\]\s*TJ[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
elseif (preg_match_all("#T[d|w|m|f]\s*(\(.*\))\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
elseif (preg_match_all("#T[d|w|m|f]\s*(\[.*\])\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
$texts = array_merge($texts, array(#implode('', $parts[1])));
}
}
function getCharTransformations(&$transformations, $stream) {
preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);
for ($j = 0; $j < count($chars); $j++) {
$count = $chars[$j][1];
$current = explode("\n", trim($chars[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
$transformations[str_pad($map[1], 4, "0")] = $map[2];
}
}
for ($j = 0; $j < count($ranges); $j++) {
$count = $ranges[$j][1];
$current = explode("\n", trim($ranges[$j][2]));
for ($k = 0; $k < $count && $k < count($current); $k++) {
if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$_from = hexdec($map[3]);
for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
} elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
$from = hexdec($map[1]);
$to = hexdec($map[2]);
$parts = preg_split("#\s+#", trim($map[3]));
for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
$transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
}
}
}
}
function getTextUsingTransformations($texts, $transformations) {
$document = "";
for ($i = 0; $i < count($texts); $i++) {
$isHex = false;
$isPlain = false;
$hex = "";
$plain = "";
for ($j = 0; $j < strlen($texts[$i]); $j++) {
$c = $texts[$i][$j];
switch($c) {
case "<":
$hex = "";
$isHex = true;
$isPlain = false;
break;
case ">":
$hexs = str_split($hex, $this->multibyte); // 2 or 4 (UTF8 or ISO)
for ($k = 0; $k < count($hexs); $k++) {
$chex = str_pad($hexs[$k], 4, "0"); // Add tailing zero
if (isset($transformations[$chex]))
$chex = $transformations[$chex];
$document .= html_entity_decode("&#x".$chex.";");
}
$isHex = false;
break;
case "(":
$plain = "";
$isPlain = true;
$isHex = false;
break;
case ")":
$document .= $plain;
$isPlain = false;
break;
case "\\":
$c2 = $texts[$i][$j + 1];
if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
elseif ($c2 == "n") $plain .= '\n';
elseif ($c2 == "r") $plain .= '\r';
elseif ($c2 == "t") $plain .= '\t';
elseif ($c2 == "b") $plain .= '\b';
elseif ($c2 == "f") $plain .= '\f';
elseif ($c2 >= '0' && $c2 <= '9') {
$oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
$j += strlen($oct) - 1;
$plain .= html_entity_decode("&#".octdec($oct).";", $this->convertquotes);
}
$j++;
break;
default:
if ($isHex)
$hex .= $c;
elseif ($isPlain)
$plain .= $c;
break;
}
}
$document .= "\n";
}
return $document;
}
}
?>

PHP-Script deletes lines in .dat file

I'm struggling with the following problem since 9 hours and can't find the solution. I'm using a very old script from here (http://www.teralaser.net/invitephp/) to build an invitation-script. I already did all the styling within the past week and it looks great, now.
the script stores the single datasets in a .dat file. Now I realized, that it always keeps just the first 125 lines of the database and deletes the rest. That's a hug he problem, as it is for an invitation with nearly 1000 persons.
After some hours I found out (better I guess), that it's the following line, which destroys it:
case "adminadd":
if (isset($_POST["pwd"])) $pwd = $_POST["pwd"];
else $pwd="";
$pwdchk = 1;
if (isset($_GET["pwd2"])) $pwdchk = strcasecmp(md5( date("z") . $admin_password),$_GET["pwd2"]);
if (strcasecmp($pwd,$admin_password) != 0 && $pwdchk != 0) {
echo "<HTML><HEAD><META HTTP-EQUIV='REFRESH' CONTENT='1; URL=$PHP_SELF?do=admin'></HEAD><BODY bgcolor='#ffffff'><CENTER><H2>Falsches Passwort!</H2>Bitte warten...</CENTER></BODY><HTML>";
exit;
}
// Get all administrator changes
$record = file($data_file);
rsort($record);
$jmlrec = count($record);
// Delete duplicates (the dum way...) and get host email if any
$hostemail = "";
for ($i = 0; $i < $jmlrec; $i++) {
if (isset($record[$i])) {
$r = $record[$i];
$row = explode("|~~|",$r);
$chkmail = $row[2];
if ($row[4] == "Gastgeber") $hostemail = $row[2];
for($j = $i+1; $j < $jmlrec; $j++) {
$r = $record[$j];
$row = explode("|~~|",$r);
if (strcasecmp($row[2],$chkmail)==0 ) { $record[$j] = ""; }
}
}
}
$updno = 0;
$err = 0;
for ($i = 0; $i < $jmlrec; $i++) { // Geändert von $jmlrec+12
// Changes ?
if (isset($_POST["vemail".$i])) $chkmail = $_POST["vemail".$i]; else $chkmail ="";
$found = -1;
if ($chkmail != "") {
// Find existing record if possible...
$recno = count($record);
for($j = 0; $j < $recno; $j++) {
$r = $record[$j];
$row = explode("|~~|",$r);
if (strcasecmp($row[2],$chkmail) == 0) { $found = $j; break; }
}
$changed = 1;
if ($found >= 0) {
// Changed ?
if ( $row[2] == $_POST["vemail".$i] && $row[3] == $_POST["vname".$i] && $row[4] == $_POST["vreply".$i] && $row[5] == $_POST["vamount".$i] &&
$row[6] == $_POST["vcompany".$i] && $row[7] == $_POST["vcomment".$i] ) $changed = 0;
}
if ( $changed != 0 ) {
$updno++;
$idx = date("YmdHis");
$tgl = date("d.m.Y H:i");
$vemail = str_replace("<","",$_POST["vemail".$i]);
$vemail = str_replace(">","",$vemail);
$vemail = str_replace("~~","--",$vemail);
$vemail = str_replace("\"",""",$vemail);
$vname = str_replace("<","",$_POST["vname".$i]);
$vname = str_replace(">","",$vname);
$vname = str_replace("~","-",$vname);
$vname = str_replace("\"",""",$vname);
$vreply = str_replace("<","",$_POST["vreply".$i]);
$vreply = str_replace(">","",$vreply);
if ($vreply != "ungesehen" && $vreply != "gesehen" && $vreply != "unentschlossen" && $vreply != "abgesagt" && $vreply != "zugesagt" && $vreply != "Gastgeber") $vreply = "ungesehen";
if ($vreply != "Gastgeber" && $vreply != "ungesehen" && $found >= 0 ) $tgl = $tgl . "*"; // admin change
$vamount = str_replace("<","<",$_POST["vamount".$i]);
$vamount = str_replace(">",">",$vamount);
$vamount = str_replace("~","-",$vamount);
$vamount = str_replace("\"",""",$vamount);
$vcomment = str_replace("<","<",$_POST["vcomment".$i]);
$vcomment = str_replace(">",">",$vcomment);
$vcomment = str_replace("~","-",$vcomment);
$vcomment = str_replace("\"",""",$vcomment);
$vcompany = str_replace("<","<",$_POST["vcompany".$i]);
$vcompany = str_replace(">",">",$vcompany);
$vcompany = str_replace("~","-",$vcompany);
$vcompany = str_replace("\"",""",$vcompany);
if (strtoupper($os) == "WIN") {
$vcomment = str_replace("\r\n","<BR>",$_POST["vcomment".$i]);
$vcomment = str_replace("\r","",$vcomment);
$vcomment = str_replace("\n","",$vcomment);
} else {
$vcomment = str_replace("\n","<BR>",$vcomment);
$vcomment = str_replace("\r","",$vcomment);
}
$validemail = 1;
if (!preg_match("/([\w\.\-]+)(\#[\w\.\-]+)(\.[a-z]{2,4})+/i", $vemail)) $validemail = 0;
if ( $vname != "" && $validemail == 0 ) { echo "Email not valid for $vname : $vemail <br>"; $err++; }
if ( $vname != "" && $validemail == 1 ) {
if ($found < 0 ) { $record[] = "\n"; $found = count($record) - 1; }
$newdata = "<?//|~~|$idx|~~|$vemail|~~|$vname|~~|$vreply|~~|$vamount|~~|$vcompany|~~|$vcomment|~~|$tgl|~~|?>\n";
$newdata = "!" . stripslashes($newdata);
$record[$found] = $newdata;
} else {
if ( $vname != "") { $updno--; $changed = 0; }
}
}
if ( $changed == 0 ) {
if ($found >= 0) $record[$found] = "!".$record[$found];
}
}
}
// Quick add
$quickadd = "";
if (isset($_POST["quickadd"]) && $_POST["quickadd"] != "") $quickadd = stripslashes($_POST["quickadd"]);
if (trim($quickadd) != "") {
$qadd = explode("\"",$quickadd);
$qaddn = count($qadd);
$nametoadd = "";
$companytoadd = "";
for($i = 0; $i < $qaddn; $i++) {
if ($i % 2 == 0) {
$q2 = explode(",",$qadd[$i]);
$q2n = count($q2);
for($j = 0; $j < $q2n; $j++) {
$mailtoadd = trim($q2[$j]);
$mailtoadd = str_replace("<","",$mailtoadd);
$mailtoadd = str_replace(">","",$mailtoadd);
$mailtoadd = str_replace("~~","--",$mailtoadd);
if ($mailtoadd != "") {
$validemail = 1;
if (!preg_match("/([\w\.\-]+)(\#[\w\.\-]+)(\.[a-z]{2,4})+/i", $mailtoadd)) $validemail = 0;
if ($validemail == 0 ) { echo "Fehler bei $mailtoadd (Keine gültige eMail-Addresse)"; $err++; } else {
if ($nametoadd == "") { $nametoaddarr = explode("#",$mailtoadd); $nametoadd = $nametoaddarr[0]; }
$idx = date("YmdHis");
$newdata = "<?//|~~|$idx|~~|$mailtoadd|~~|$nametoadd|~~|ungesehen|~~|1|~~|$companytoadd|~~||~~|?>\n";
$newdata = "!" . stripslashes($newdata);
$record[]= $newdata;
$nametoadd = "";
$companytoadd = "";
}
}
}
}
if ($i % 2 == 1) {
$data = $qadd[$i];
list($nametoadd, $companytoadd) = explode(";", $data);
$nametoadd = str_replace("<","" ,$nametoadd);
$nametoadd = str_replace(">","" ,$nametoadd);
$nametoadd = str_replace("~","-",$nametoadd);
$companytoadd = str_replace("|","" ,$companytoadd);
$companytoadd = str_replace("<","" ,$companytoadd);
$companytoadd = str_replace(">","" ,$companytoadd);
$companytoadd = str_replace("~","-",$companytoadd);
}
}
}
/* Hier liegt der tote Fuchs begraben!!! */
$jmlrec = count($record);
$update_data = fopen($data_file,"w");
if (strtoupper($os) == "UNIX") {
if (flock($update_data,LOCK_EX)) {
for ($j=0; $j<$jmlrec; $j++) {
if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
}
flock($update_data,LOCK_UN);
}
} else {
for ($j=0; $j<$jmlrec; $j++) {
if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
}
}
fclose($update_data);
I'm looking forward for some ideas. I'll love to invite you for a coffee... ;) I can also send the whole script, if someone is interested.
Best!
Philipp
EDIT: I'm still totally lost in the code, so I want to share the other parts of the code with you and maybe someone finds the problem. ;) Thanks!
$jml_page = intval($jmlrec/$max_entry_per_page);
$sisa = $jmlrec%$max_entry_per_page;
if ($sisa > 0) $jml_page++;
// Set $nomaybe, $nono, $noyes DEBUG
$nomaybe = 0;
$nono = 0;
$noyes = 0;
$countmaybe = 0;
$countno = 0;
$countyes = 0;
$counttotal = 0;
$no = 0;
if ($jmlrec == 0) echo "<tr><td align='center'>Noch keine Einträge.</td></tr>";
$w = 0; //--Color
for ($i=0; $i<$max_entry_per_page; $i++) {
// Find the lowest next possible record.
$no = $jmlrec + 1;
if ($nomaybe < $jmlrec ) $no = $nomaybe + 1;
if ($nono < $no - 1 ) $no = $nono + 1;
if ($noyes < $no - 1 ) $no = $noyes + 1;
// Check this is valid.
do {
while (($no < $jmlrec + 1) && (!isset($record[$no-1]) || $record[$no-1] == "")) { $no++; }
$recno = $no - 1;
$cont = 0;
if ($no < $jmlrec + 1) {
$row = explode("|~~|",$record[$recno]);
$vr = $row[4];
$cont = 0;
if (($vr == "ungesehen" || $vr == "gesehen" || $vr == "unentschlossen") && ($no <= $nomaybe)) $cont = 1;
if (($vr == "abgesagt" ) && ($no <= $nono)) $cont = 1;
if (($vr == "zugesagt" || $vr == "Gastgeber") && ($no <= $noyes)) $cont = 1;
if ($cont == 1) $no++;
}
} while ($cont == 1);
if (isset($record[$recno]) && $record[$recno] != "") {
if ($w==0) {
$warna = $table_content_1a;
$warna2 = $table_content_1b;
$w=1;
} else {
$warna = $table_content_2a;
$warna2 = $table_content_2b;
$w=0;
}
do {
$nomaybe++;
$recno = $nomaybe-1;
$row = explode("|~~|",$record[$recno]);
}
while ($nomaybe <= $jmlrec && $row[4] != "ungesehen" && $row[4] != "gesehen" && $row[4] != "unentschlossen" );
if ($row[4] == "ungesehen" || $row[4] == "gesehen" || $row[4]=="unentschlossen") {
echo "<tr><td>$row[3]</td>";
if (isset($row[2]) && $row[2] != "" ) echo "<td><p>$row[2]</p></td>"; else echo "<td><p></p></td>";
if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
$countmaybe++;
}
if ($row[4] == "gesehen") echo "<td><b>gesehen</b></td><td>$row[8]</td>";
if ($row[4] == "unentschlossen") echo "<td><b>unentschlossen</b></td><td>$row[8]</td>";
if ($row[4] == "ungesehen") echo "<td><b>ungesehen</b></td><td></td>";
echo "<td><span class='date'><b> </b></span></td>";
if (isset($row[7]) && ($row[4]=="gesehen" || $row[4]=="unentschlossen" || $row[4]=="ungesehen" ) && $row[7] != "")
echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
do {
$nono++;
$recno = $nono-1;
$row = explode("|~~|",$record[$recno]);
}
while ($nono <= $jmlrec && $row[4] != "abgesagt" && $row != "gesehen" && $row[5] != "unentschlossen" );
$row = explode("|~~|",$record[$recno]);
if ($row[4] == "abgesagt" ) { echo "<tr><td>$row[3]</td><td><p>$row[6]</p></td><td><b>abgesagt</b></td><td>$row[8]</td>";
echo "<td><span class='date'><b> </b></span></td>";
if (isset($row[7]) && $row[7] != "") echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
$countno++;
}
do {
$noyes++;
$recno = $noyes-1;
$row = explode("|~~|",$record[$recno]);
} while ($noyes <= $jmlrec && $row[4] != "zugesagt" && $row[4] != "Gastgeber" );
if ($row[4] == "zugesagt" || $row[4] == "Gastgeber" ) {
echo "<tr><td>$row[3]";
if ($row[4] == "Gastgeber") print " (Gastgeber) ";
echo "</td>";
if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
echo "<td><b>zugesagt</b></td><td>$row[8]</td>";
if (isset($row[5]) && $row[5] != "") echo "<td><p>$row[5]</p></td>"; else echo "<td><p>1</p></td>";
if (isset($row[7]) && $row[7] != "" ) echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
$countyes++;
if (isset($row[5]) && $row[5] != "") $counttotal = $counttotal + intval($row[5]);
}
} //--end if
} //--end for
It would appear that updated entries are marked by prepending them with !. This loop appears to only be saving the updates, not every record.
Perhaps there was a typo - fopen with mode "w" will truncate the data file at 0 bytes before writing, if only the updated records are to be written, shouldn't that be mode "a" to append them to the existing file?
It is likely there is also a limit on how large a data file is allowed to grow, make sure you have set it large enough to allow 1000 records.

php regular comment remove

how remove multi line comment ? ( /* comment php */) from file .php
how remove single comment ? ( // coment ) from file.php
how remove enter end line ?
sample :
single comment line
$G["url"] = "http://".$_SERVER["HTTP_HOST"]
multi comment line
/*
* 310
* - "::"
* - $col
*/
Try using the code posted below, it even ignores comment tokens on strings like " /* comment2 */ "
<?php
$fileIn = "src.php";
$fileOut = "srcCompressed.php";
$text = file_get_contents($fileIn);
$text = compress_php_src($text);
file_put_contents($fileOut, $text);
function compress_php_src($src) {
// Whitespaces left and right from this signs can be ignored
static $IW = array(
T_CONCAT_EQUAL, // .=
T_DOUBLE_ARROW, // =>
T_BOOLEAN_AND, // &&
T_BOOLEAN_OR, // ||
T_IS_EQUAL, // ==
T_IS_NOT_EQUAL, // != or <>
T_IS_SMALLER_OR_EQUAL, // <=
T_IS_GREATER_OR_EQUAL, // >=
T_INC, // ++
T_DEC, // --
T_PLUS_EQUAL, // +=
T_MINUS_EQUAL, // -=
T_MUL_EQUAL, // *=
T_DIV_EQUAL, // /=
T_IS_IDENTICAL, // ===
T_IS_NOT_IDENTICAL, // !==
T_DOUBLE_COLON, // ::
T_PAAMAYIM_NEKUDOTAYIM, // ::
T_OBJECT_OPERATOR, // ->
T_DOLLAR_OPEN_CURLY_BRACES, // ${
T_AND_EQUAL, // &=
T_MOD_EQUAL, // %=
T_XOR_EQUAL, // ^=
T_OR_EQUAL, // |=
T_SL, // <<
T_SR, // >>
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
);
if(is_file($src)) {
if(!$src = file_get_contents($src)) {
return false;
}
}
$tokens = token_get_all($src);
$new = "";
$c = sizeof($tokens);
$iw = false; // ignore whitespace
$ih = false; // in HEREDOC
$ls = ""; // last sign
$ot = null; // open tag
for($i = 0; $i < $c; $i++) {
$token = $tokens[$i];
if(is_array($token)) {
list($tn, $ts) = $token; // tokens: number, string, line
$tname = token_name($tn);
if($tn == T_INLINE_HTML) {
$new .= $ts;
$iw = false;
} else {
if($tn == T_OPEN_TAG) {
if(strpos($ts, " ") || strpos($ts, "\n") || strpos($ts, "\t") || strpos($ts, "\r")) {
$ts = rtrim($ts);
}
$ts .= " ";
$new .= $ts;
$ot = T_OPEN_TAG;
$iw = true;
} elseif($tn == T_OPEN_TAG_WITH_ECHO) {
$new .= $ts;
$ot = T_OPEN_TAG_WITH_ECHO;
$iw = true;
} elseif($tn == T_CLOSE_TAG) {
if($ot == T_OPEN_TAG_WITH_ECHO) {
$new = rtrim($new, "; ");
} else {
$ts = " ".$ts;
}
$new .= $ts;
$ot = null;
$iw = false;
} elseif(in_array($tn, $IW)) {
$new .= $ts;
$iw = true;
} elseif($tn == T_CONSTANT_ENCAPSED_STRING
|| $tn == T_ENCAPSED_AND_WHITESPACE)
{
if($ts[0] == '"') {
$ts = addcslashes($ts, "\n\t\r");
}
$new .= $ts;
$iw = true;
} elseif($tn == T_WHITESPACE) {
$nt = #$tokens[$i+1];
if(!$iw && (!is_string($nt) || $nt == '$') && !in_array($nt[0], $IW)) {
$new .= " ";
}
$iw = false;
} elseif($tn == T_START_HEREDOC) {
$new .= "<<<S\n";
$iw = false;
$ih = true; // in HEREDOC
} elseif($tn == T_END_HEREDOC) {
$new .= "S;";
$iw = true;
$ih = false; // in HEREDOC
for($j = $i+1; $j < $c; $j++) {
if(is_string($tokens[$j]) && $tokens[$j] == ";") {
$i = $j;
break;
} else if($tokens[$j][0] == T_CLOSE_TAG) {
break;
}
}
} elseif($tn == T_COMMENT || $tn == T_DOC_COMMENT) {
$iw = true;
} else {
if(!$ih) {
$ts = strtolower($ts);
}
$new .= $ts;
$iw = false;
}
}
$ls = "";
} else {
if(($token != ";" && $token != ":") || $ls != $token) {
$new .= $token;
$ls = $token;
}
$iw = true;
}
}
return $new;
}
?>
Credits to gelamu function compress_php_src().
There's no reason to remove comments from your code. The overhead from processing them is so incredibly minimal that it doesn't justify the effort.
I think you are probably looking for php_strip_whitespace().

Categories