What I am trying to do is read a value from blankVoteOB.txt, delete the value and repeat this process multiple times.
blankVote.php
global $cur_random;
for ($i=0; $i<2; $i++){
include 'readWriteRandomBV.php';
$random[$i]=$cur_random;
echo $random[$i];
echo ',';
}
readWriteRandomBV.php (reads current line from file blankVoteOB.txt and then deletes the line)
<?php
$dir = "blankVoteOB.txt";
$file = fopen($dir, "r") or exit("Unable to open file!");
global $lines;
global $line_no;
global $all_lines;
global $writelines;
global $cur_random;
$lines = "";
$line_no=0;
while(!feof($file)) {
$line_no++;
$line = fgets($file);
$all_lines .= $line."<br>";
if($line_no > 1)
$writelines .= $line;
else{
$curran = trim($line);
$cur_random = $curran;
}
}
fclose($file);
$fh = fopen($dir, 'w') or die("ERROR! Cannot open $file file!");
fwrite($fh, $writelines);
fclose($fh);
?>
Before running the PHPs, blankVoteOB.txt looks like this:
313328804459
159078851698
226414688415
380287830671
301815692106
2991355110
After being ran, it becomes:
159078851698
226414688415
380287830671
301815692106
2991355110
226414688415
380287830671
301815692106
2991355110
What I want is:
226414688415
380287830671
301815692106
2991355110
What am I doing wrong here?
I suggest you use an array to store the ballots, and then use array_shift to get the first item from the array.
I prefer using classes so I made a lottery class which allows you to "draw" the first item in the array.
If you run the code below and match the text output to the code you can see what it does.
See it live here: https://ideone.com/T8stdB
<?php
namespace Lottery;
class Lotto {
protected $lots;
public function __construct($lots = [])
{
$this->lots = $lots;
}
public function draw() {
return array_shift($this->lots);
}
}
namespace BallotGuy;
use Lottery\Lotto;
$lotto = new Lotto([313328804459,
159078851698,
226414688415,
380287830671,
301815692106,
2991355110,
]);
echo "Lotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
echo "Drawn: " . $lotto->draw()."\n";
echo "\nLotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
$saved = serialize($lotto);
//file_put_contents('ballots.txt',$saved);
/**
* setting to null to emulate script ending
*/
$lotto = null;
echo "Lotto set to null 'script' ends sort to speak here\n";
echo "\nLotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
echo "Loading lotto from file\n";
//$saved = file_get_contents('ballots.txt');
$lotto = unserialize($saved);
echo "\nLotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
echo "Drawn: ". $lotto->draw()."\n";
echo "\nLotto status at this point\n";
echo "===========================================================\n";
var_dump($lotto);
echo "===========================================================\n";
A version without the superfluos var_dumping
See it live https://ideone.com/YNKIM4
<?php
namespace Lottery;
class Lotto {
protected $lots;
public function __construct($lots = [])
{
$this->lots = $lots;
}
public function draw() {
return array_shift($this->lots);
}
}
namespace BallotGuy;
use Lottery\Lotto;
/**
* initialize lotto object
*/
$lotto = new Lotto([313328804459,
159078851698,
226414688415,
380287830671,
301815692106,
2991355110,
]);
echo "Drawn: " . $lotto->draw()."\n";
echo "Writing lotto to file. Ending script(j/k)\n";
$saved = serialize($lotto);
file_put_contents('ballots.txt',$saved);
/**
* setting to null to emulate script ending
*/
$lotto = null;
$saved = null;
echo "Loading lotto from file\n";
$saved = file_get_contents('ballots.txt');
$lotto = unserialize($saved);
echo "Drawn: ". $lotto->draw()."\n";
var_dump($lotto);
Related
I have a path in my Laravel project (app/data/report) with 2 files: refresh_numbers.php''' and '''numbers_temp.php
I'm trying to run refresh numbers as a way to run one function within numbers_temp like so:
refresh_numbers.php
<?php
require 'Numbers_temp.php';
echo "Beginning \n\n";
Numbers_temp::refresh();
echo "\n Finished \n";
?>
Numbers_temp.php
<?php
namespace app/data/report;
class Numbers_temp
{
function refresh()
{
$sql = "select C,S,P,Q FROM data";
$result_set = DB::runQuery_simple($sql);
$log = "";
foreach ($result_set as $row) {
$log .= "EXECUTING: $sql -- ";
$res = $this->add_quota($row['S'], $row['P'], $row['Q'], $row['C']);
$log .= "$res \n";
}
return $log;
}
}
But when I run refresh_numbers.php I get an error on line 6 that Class Numbers_temp can't be found?
It just doesn't know about the namespace. Add a use statement:
use app\data\report\Numbers_temp;
I'm writing a custom import script for SuiteCRM and I'm getting the error:
Warning: array_combine() expects parameter 2 to be array, boolean
given in /var/www/html/afscmedbv5test/custom/wimporter/newimporter.php
on line 162
My Script is as follows:
<?php
if (!defined('sugarEntry') || !sugarEntry) die ('Not a Valid Entry Point!');
$date = new DateTime();
echo '<H2>Wilderness Import Started</h2>';
echo $date->format('r').'<br>';
echo '-----------------------------------------------------------------------
--------------------------------------------------------------<br>';
require_once("include/utils/sugar_file_utils.php");
WildernessImportJob();
die();
function var_dump_ret($mixed = null) {
ob_start();
var_dump($mixed);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
function time_elapsed()
{
static $first = null;
static $previous = null;
$now = microtime(true);
if ($first == null) $first = $now;
if ($previous != null)
echo '--- Partial ' . round(($now - $previous), 2) . ', Total ' . round(($now
- $first), 2) . ' ---'; // 109s
$ret = round(($now - $previous), 2);
$previous = $now;
return $ret;
}
function myLog ($str2log)
{
file_put_contents('./zlog_'.date("j.n.Y").'.txt', date("H:i:s", time())."
".$str2log.PHP_EOL, FILE_APPEND);
}
function calcDelta($a1, $a2)
{
//combine into a nice associative array:
$delta=Array();
foreach ($a1 as $key=>$value)
{
if ($a1[$key] != $a2->$key)
$delta[] = array($key => ("Was ". $a1[$key]. ", became " . $a2->$key));
}
$num = count($data);
if (empty($a1)) $delta[] = array("a1" => ("Was empty"));
if (empty($a2)) $delta[] = array("a2" => ("Was empty"));
return $delta;
}
require_once("include/utils/sugar_file_utils.php");
function fillPerson($record, &$person)
{
// $record is what is being imported from CSV
// $person is the bean about to be filled and going into the SuitCRM DB. It
may be new or not, depending on whether it exists in the DB previously.
// name: only updates if not existant yet, because it's the key we use for
search, and because names are more complex with parts
if ($person->full_name_c == "") {
$recordname = $record["FULL NAME"]; // != "") ? $record["FULL NAME"] : "
[To-be-filled]");
//echo $prefix;
$recordname = str_replace(" ", " ", $recordname);
echo $recordname;
$parts = explode(" ", $recordname);
$person->last_name = array_pop($parts);
$person->first_name = $parts[0];
$person->name = $person->first_name . " " . $person->last_name;
$person->full_name_c = $record["FULL NAME"]; // custom field created in
Studio
}
//$datanasc = DateTime::createFromFormat('!m/d/Y', $record["PPE"]);
// $datasnasc->setTime(0, 0);
// $person->ppe_date_c = ($datanasc == false) ? "" : $datanasc->format('m-d-
Y');
//$person->ppe_date_c = $record["PPE"];
//print_r($person);
var_dump($person);
}
// finish by making a complete analysis of what changed:
return calcDelta($person->fetched_row, $person);
function GetOrCreateMember ($the_name)
{
//Check if the fullname is null
if ($the_name != "")
{
$person = BeanFactory::getBean("locte_Members");
$person = $person->retrieve_by_string_fields(array('full_name_c' =>
$the_name));
if (is_null($person))
{
//get members bean
$person = BeanFactory::newBean("locte_Members");
//set full_name_c to the_name variable
$person->full_name_c = $the_name;
// $person->lcl_employee_id = $personEmployeeID;
$person_name = str_replace(" ", " ", $the_name);
$parts = explode(" ", $person_name);
$person->last_name = array_pop($parts);
$person->first_name = $parts[0];
//combine first and last name to populate the fullname field
$person->name = $person->first_name . " " . $person->last_name;
$person_id = $person->save();
// add new duespayment to member record
// $rosterDuesPayments = BeanFactory::getBean('Dues Payments')-
>retrieve_by_string_fields(array('name'=> $duesEmployeeID));
// $person->load_relationship('locte_Members_adues_dues'); //confirm
relationship name in cache
// $person->dues_payments->add($rosterDuesPayments->id);
}
return $person;
}
return null;
}
function WildernessImportJob()
{
try
{
time_elapsed();
$GLOBALS['log']->info('Wilderness Import');
$config = new Configurator();
$config->loadConfig();
$xmlDataDir = 'custom/wimporter/ToImport'; //$config->config['WildernessImporter_DataFilePath'];
$GLOBALS['log']->info("Wilderness Import: Scanning XML Data dir $xmlDataDir...");
echo("<h3>Wilderness Import: Scanning XML Data dir $xmlDataDir...<br /></h3>");
$directoryContent = scandir($xmlDataDir);
$GLOBALS['log']->info("Wilderness Import: Scanning XML Data dir $xmlDataDir... [Found " . count($directoryContent) . " files]");
echo("<h3>Wilderness Import: Scanning XML Data dir $xmlDataDir... [Found " . count($directoryContent) . " files]</h3><br />");
foreach ($directoryContent as $itemFile)
{
if (is_dir($xmlDataDir . DIRECTORY_SEPARATOR . $itemFile)) continue;
if (strcasecmp(substr($itemFile, -4), ".csv") != 0) continue;
$GLOBALS['log']->info("Wilderness Import: Processing $itemFile file...");
myLog("---------------------------------------------------");
myLog("Wilderness Import: Processing $itemFile file...");
myLog("----------------------------------------------------");
echo("<h4>---------------------------------------------------------------</h4>");
echo("<h4>Wilderness Import: Processing $itemFile file...</h4>");
echo("<h4>---------------------------------------------------------------</h4>");
$oFile = fopen($xmlDataDir . DIRECTORY_SEPARATOR . $itemFile, 'r');
if ($oFile !== FALSE)
{
// read entire file at once:
// expected separator is ",", expected encoding is UTF-8 without BOM (BOM is 3 weird characters in beginning of file)
while (($data[] = fgetcsv($oFile, 0, ',')) !== FALSE) { }
echo('File opened..... <br /> <br />');
fclose($oFile);
//combine into a nice associative array:
$arow=Array();
echo('Building CSV File Row Array <br /><br />');
$fields = array_shift($data);
echo('Building CSV Header Fields Array as shown below:<strong> <br /><br />');
echo implode(", ", $fields) . "</strong><br /><br />\n";
foreach ($data as $i=>$arow)
{
$GLOBALS['log']->info("Wilderness Import: array_combine " . $i);
$data[$i] = array_combine($fields, $arow);
}
unset($arow); // **********************************************! ! ! !! ! ! ! ! ! !! !
$num = count($data);
echo('Build Full Array of Roster to be Imported Complete. Entries to be imported are shown below <br /><br />');
for ($row=0; $row < $num - 1 ; $row++)
{ // normal bounds: from 0 to $num
//$num is the number of lines including header in csv file
echo "<strong>Filename: $itemFile | Roster Import, Row" . ($row + 1) . ":</strong><br />\n";
$GLOBALS['log']->info("Wilderness Import: Importing " . $data[$row]["FULL NAME"]);
// echo("<strong>Importing Roster Row #: ". ($row + 1) . "<br />" . "Local Number " . $data[$row]["AFFILIATE"] . "<br />" . "Employee: " . $data[$row]["FULL NAME"] . "</strong><br /><br />");
echo "<strong><table>\n";
foreach ($fields as $field) {
//echo "<tr><td>" . $field . "</td><td>" . $data[$row][$field] . "</td><td>" . $data[$row+1][$field] . "</td><td>" . $data[$row+2][$field] . "</td></tr>\n";
}
echo "</table>\n";
echo "File Row Data: ";
echo implode(", ", $data[$row]) . "</strong><br /><br />\n";
$Member = BeanFactory::getBean("locte_Members");
$FullName=$Member->full_name_c;
//$myfield_defs = $Member->getFieldDefinitions(); // just to help while developing
//foreach($myfield_defs as $def) echo $def["name"] . "<br />\n";
$Member=$Member->retrieve_by_string_fields(array('full_name_c' => $data[$row]["FULL NAME"]));
if (is_null($Member)) {
$Member = BeanFactory::newBean("locte_Members");
$delta = fillPerson($data[$row], $Member, ""); //->full_name_c, "FULL NAME");
}
if (count($delta)) {
$Member_id = $Member->save();
}
}
// Records have been saved: from this point on, only work on relationships:
$GLOBALS['log']->info('End: Wilderness Import');
myLog('End: Wilderness Import');
time_elapsed();
return true;
}
}
} catch (Exception $e)
{
$GLOBALS['log']->fatal("Wilderness Import: Exception " . $e->getMessage());
myLog("Wilderness Import: Exception " . $e->getMessage());
echo '\n\nCaught exception: ', $e->getMessage(), "\n";
return false;
}
}
It does return info from both the database and the csv file.
Image of error below.
Error - Capture from browser
Help always appreciated :)
This script is throwing error because the parameter $arow given to array_combine is not an array. So a check should be there to check whether $arow is an array or not.
Try code following:
foreach ($data as $i => $arow) {
$GLOBALS['log']->info("Wilderness Import: array_combine " . $i);
if (is_array($arow)) {
$data[$i] = array_combine($fields, $arow);
}
}
Read more about array_combine
Update
Code you are using to read csv need to be changed. Second parameter in fgetcsv must be greater than the longest line (in characters) to be found in the CSV file. So replace code
while (($data[] = fgetcsv($oFile, 0, ', ')) !== FALSE) {
}
with
while (($data[] = fgetcsv($oFile, 10, ', ')) !== FALSE) {
}
Read more about fgetcsv
This block is doing the saving of records using the beanfactory.... I think :|
$Member = BeanFactory::getBean("locte_Members");
$FullName=$Member->full_name_c;
//$myfield_defs = $Member->getFieldDefinitions(); // just to help while developing
//foreach($myfield_defs as $def) echo $def["name"] . "<br />\n";
$Member=$Member->retrieve_by_string_fields(array('full_name_c' => $data[$row]["FULL NAME"]));
if (is_null($Member)) {
$Member = BeanFactory::newBean("locte_Members");
$delta = fillPerson($data[$row], $Member->full_name_c, "FULL NAME");
var_dump($arow);
}
if (count($delta)) {
$Member_id = $Member->save();
}
}
// Records have been saved: from this point on, only work on relationships:
Here's the output of the script in the browser with field definition dump
Wilderness Import Started
Tue, 19 Jun 2018 02:05:56 -0400
Wilderness Import: Scanning XML Data dir custom/wimporter/ToImport...
Wilderness Import: Scanning XML Data dir custom/wimporter/ToImport... [Found 3 files]
Wilderness Import: Processing L1554v4.csv file...
File opened.....
Building CSV File Row Array
Building CSV Header Fields Array as shown below:
LAST NAME, FIRST NAME, FULL NAME
Build Full Array of Roster to be Imported Complete. Entries to be imported are shown below
Filename: L1554v4.csv | Roster Import, Row1:
LAST NAME BUTLER
FIRST NAME BRIANA
FULL NAME BUTLER BRIANA
File Row Data: BUTLER, BRIANA, BUTLER BRIANA
-id
-name
-date_entered
-date_modified
-modified_user_id
-modified_by_name
-created_by
-created_by_name
-description
-deleted
-securitygroup
-securitygroup_display
-created_by_link
-modified_user_link
-assigned_user_id
-assigned_user_name
-assigned_user_link
-additionalusers
-additionalusers_listview
-salutation
-first_name
-last_name
-full_name
-title
-photo
-department
-do_not_call
-phone_home
-email
-phone_mobile
-phone_work
-phone_other
-phone_fax
-email1
-email2
-invalid_email
-email_opt_out
-primary_address_street
-primary_address_street_2
-primary_address_street_3
-primary_address_city
-primary_address_state
-primary_address_postalcode
-primary_address_country
-alt_address_street
-alt_address_street_2
-alt_address_street_3
-alt_address_city
-alt_address_state
-alt_address_postalcode
-alt_address_country
-assistant
-assistant_phone
-email_addresses_primary
-email_addresses
-email_addresses_non_primary
-lcl_birthdate
-lcl_affiliate_number
-member_signature
-ssn
-staff_rep
-mem_join_date
-class_title
-steward
-olo_code
-enterprise_id
-member_card
-address_map
-member_status
-primary_language
-english_speaking
-annual_salary
-currency_id
-state_hire_date
-dues_frequency
-card_sent_date
-gender
-disabilities
-marital_status
-executive_board
-affiliate_type
-middle_name
-name_suffix
-mailableflag
-no_mailflag
-apt_number
-zip4
-infosrc
-lcl_phone_collected
-lcl_hasmobile
-lcl_hasemail
-lcl_member_status
-backofficeassistants
-backoffice_assistant_phonoe
-backoffice_assistant_email
-lcl_employercontact
-lcl_work_dept
-lcl_work_location
-lcl_jobclasstitle
-lcl_employee_id
-lcl_join_date
-lcl_sig_auth
-lcl_dues_status
-lcl_on_probation
-lcl_prob_end_date
-lcl_prob_term
-afs_signed_by
-afs_region_number
-afemp_employers_id_c
-afs_employer
-primary_address_county
-locte_members_adues_dues
-full_name_c
-locte_members_afcbu_cbu
-locte_members_afcbu_cbu_name
-locte_members_afcbu_cbuafcbu_cbu_idb
-ppe_date_c
--- Partial 0.01, Total 0.01 ---
Im asking for help to add a counter for my link redirect code.
The link redirect code looks like this:
<?
$id = preg_replace("/[^0-9]/","",$_GET['id']);
$x[101] = "http://www.ebay.com";
$x[102] = "http://www.google.com";
$x[103] = "http://wikileaks.org";
$x[104] = "http://potato.com";
if (isset($x[$id])){}
else {
die (header("Location: http://www.google.com"));}
header("Location: $x[$id]");
exit;
?>
That code lies in a file called link.php, and then I use links like www.mysite.com/links.php?id=103 . When someone clicks that link the code will direct them to http://wikileaks.org.
Now what im looking for is to count the hits for the different links 101 to 104 without using mysql.
Maybe something like this?
http://www.stevedawson.com/scripts/text-counter.php
<?php
if (file_exists('count_file.txt'))
{
$fil = fopen('count_file.txt', r);
$dat = fread($fil, filesize('count_file.txt'));
echo $dat+1;
fclose($fil);
$fil = fopen('count_file.txt', w);
fwrite($fil, $dat+1);
}
else
{
$fil = fopen('count_file.txt', w);
fwrite($fil, 1);
echo '1';
fclose($fil);
}
?>
I have no idea how to add it to my redirect code, and also hopefully make it work for all the links. Counting each link, and hopefully not creating a 100 different text files, as the real site has lots more links than my example code above. Any ideas?
How about adding it to an array, increment it and serialize the array to a file?
<?php
$id = preg_replace("/[^0-9]/","",$_GET['id']);
$x[101] = "http://www.ebay.com";
$x[102] = "http://www.google.com";
$x[103] = "http://wikileaks.org";
$x[104] = "http://potato.com";
// Open and lock file
$filename = 'count_file.dat';
$fil = fopen($filename, 'c+');
while(!flock($fil, LOCK_EX))
usleep(rand(1, 10000));
// Read data
$dat = unserialize( fread($fil, filesize($filename) ) );
if ( !is_array( $dat ) )
$dat = array();
// Count
if ( isset($dat[$id]) )
$dat[$id]++;
else
$dat[$id] = 1;
// Write and unlock file
fseek( $fil, 0 );
fwrite($fil, serialize($dat));
fflush($fil);
flock($fil, LOCK_UN);
fclose( $fil );
if (isset($x[$id])) {
header("Location: $x[$id]");
} else {
header("Location: http://www.google.com");
}
exit;
// Count hits
foreach( $x as $k => $v )
{
if( $dat[$k] > 0 )
echo 'Site ' . $v . ' has ' . $dat[$k] . ' hits!' . "<br>\n";
else
echo 'Site ' . $v . ' has no hits!' . "<br>\n";
}
?>
I have added locking, so two instances can not read/write to the same file at the same time, giving a corrupt file!
Final Edit Working version which saves hits into an array, serializes them to a single file and reads the array back next time. Also a hits display example at the end. Simplified version!
Just add it. What is the problem?
<?php
$id = preg_replace("/[^0-9]/","",$_GET['id']);
$x[101] = "http://www.ebay.com";
$x[102] = "http://www.google.com";
$x[103] = "http://wikileaks.org";
$x[104] = "http://potato.com";
if (isset($x[$id])){
$filename = 'count_file_'.$id.'.txt';
}else{
$filename = 'count_file_default.txt';
}
if (file_exists($filename))
{
$fil = fopen($filename, r);
$dat = fread($fil, filesize($filename));
fclose($fil);
$fil = fopen($filename, w);
fwrite($fil, $dat+1);
}
else
{
$fil = fopen($filename, w);
fwrite($fil, 1);
fclose($fil);
}
if (isset($x[$id])){}
else {
die (header("Location: http://www.google.com"));}
header("Location: $x[$id]");
exit;
?>
EDIT: I just saw you don't want to have as many text files. Maybe your best choice would be to store it in a database. Or maybe store json in your txt file, read it, modify it, and write it again.
I'm trying to create multiple .php files using php itself.
I want to put some code into a file; most of code is the same but only one or two variables that I wanted to be dynamic. I mean every file that I make are exactly like each other the only difference between theme is one variable.
My function is this:
function generate_corn_files()
{
$C = $GLOBALS['C'];
$db = $GLOBALS['db'];
//delete all contents of folder
RemoveDir($C->INCPATH.'cron/feed/', false);
$res = $db->query('SELECT id FROM category ');
while($cat = $db->fetch_object($res)) {
$id = $cat->id;
$open_output = <<<'PHP'
<?php
$outter_id = $id;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
$fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
fwrite($fp, $open_output);
fclose($fp);
}
}
I tried to put content of file using heredoc but I want to $id in $outter_id = $id; be equal to $id = $cat->id;
it's a variable outside of heredoc I can't make it work inside of it !
Are there any other solutions to make it work ?
You aren't using HEREDOC syntax but rather NOWDOC syntax. If you use HEREDOC, all variables inside will be evaluated, so you will have to escape with \$ the variables you don't want evaluated.
$open_output = <<<PHP
<?php
\$outter_id = $id;
if(\$example = true){
echo 'test';
echo \$C->INCPATH;
}
?>
PHP;
Or, you can stick with NOWDOC, use a placeholder, and replace it afterwards.
$open_output = <<<'PHP'
<?php
$outter_id = %%%id%%%;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
str_replace("%%%id%%%", $id, $open_output);
Maybe this could inspire you
function generate_corn_files()
{
$C = $GLOBALS['C'];
$db = $GLOBALS['db'];
//delete all contents of folder
RemoveDir($C->INCPATH.'cron/feed/', false);
$res = $db->query('SELECT id FROM category ');
while($cat = $db->fetch_object($res)) {
$id = $cat->id;
$open_output = <<<'PHP'
<?php
$outter_id = $id;
if($example = true){
echo 'test';
echo $C->INCPATH;
}
?>
PHP;
$php_var_name_pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
$open_output = preg_replace_callback(
$php_var_name_pattern,
function($matches) {
if(isset($GLOBALS[$matches[1]])) {
if(is_string($GLOBALS[$matches[1]])) {
return '\''.$GLOBALS[$matches[1]].'\'';
} else {
return $GLOBALS[$matches[1]];
}
} else {
return $matches[0];
}
},
$open_output);
$fp=fopen($C->INCPATH.'cron/feed/filename_'.$id.'.php','w');
fwrite($fp, $open_output);
fclose($fp);
}
}
I am working on a php script to pull quest data from wowhead, particularly what starts and ends the quest, whether it is an item or a npc, and what its id or name is, respectively. This is the relevant portion of the whole script, with the rest involving database insertion. This is the completed snippet of code I came up with if anyone is interested. Also, seeing as this will run about 15,000 times, is this the best method of obtaining/storing the data?
<?php
$quests = array();
//$questlimit = 14987;
$questlimit = 5;
$currentquest = 1;
$questsprocessed = 0;
while($questsprocessed != $questlimit)
{
echo "<br>";
echo " Start of iteration: ".$questsprocessed." ";
echo "<br>";
echo " Attempting to process quest: ".$currentquest." ";
echo "<br>";
$quests[$currentquest] = array();
$baseurl = 'http://wowhead.com/quest=';
$fullurl = $baseurl.$currentquest;
$data = drupal_http_request($fullurl);
$queststartloc1 = strpos($data->data, 'quest_start');
$queststartloc2 = strpos($data->data, 'quest_end');
if($queststartloc1==false)
{$currentquest++; echo "No data for this quest"; echo "<br>"; continue;}
$questendloc1 = strpos($data->data, 'quest_end');
$questendloc2 = strpos($data->data, 'x5DDifficulty');
$startcaptureLength = $queststartloc2 - $queststartloc1;
$endcaptureLength = $questendloc2 - $questendloc1;
$quest_start_raw = substr($data->data,$queststartloc1, $startcaptureLength);
$quest_end_raw = substr($data->data, $questendloc1, $endcaptureLength);
$startDecoded = preg_replace('~\\\\x([A-Fa-f0-9]{2})~e', 'chr("0x$1")', $quest_start_raw);
$endDecoded = preg_replace('~\\\\x([A-Fa-f0-9]{2})~e', 'chr("0x$1")', $quest_end_raw);
$quests[$currentquest]['Start'] = array();
$quests[$currentquest]['End'] = array();
if(strstr($startDecoded, 'npc'))
{
$quests[$currentquest]['Start']['Type'] = "npc";
preg_match('~npc=(\d+)~', $startDecoded, $startmatch);
}
else
{
$quests[$currentquest]['Start']['Type'] = "item";
preg_match('~item=(\d+)~', $startDecoded, $startmatch);
}
$quests[$currentquest]['Start']['ID'] = $startmatch[1];
if(strstr($endDecoded, 'npc'))
{
$quests[$currentquest]['End']['Type'] = "npc";
preg_match('~npc=(\d+)~', $endDecoded, $endmatch);
}
else
{
$quests[$currentquest]['End']['Type'] = "item";
preg_match('~item=(\d+)~', $endDecoded, $endmatch);
}
$quests[$currentquest]['End']['ID'] = $endmatch[1];
//var_dump($quests[$currentquest]);
echo " End of iteration: ".$questsprocessed." ";
echo "<br>";
echo " Processed quest: ".$currentquest." ";
echo "<br>";
$currentquest++;
$questsprocessed++;
}
?>
These are called "escape sequences". Normally, they're used to represent characters not printable otherwise, but can encode any character. In php, you can decode them like this:
$text = '
quest_start\\x5DStart\\x3A\\x20\\x5Bitem\\x3D16305\\x5D\\x5B\\x2Ficon\\x5D\\x5B\\x2Fli\\x5D\\x5Bli\\x5D\\x5Bicon\\x20name\\x3Dquest_end\\x5DEnd\\x3A\\x20\\x5Burl\\x3D\\x2Fnpc\\x3D12696\\x5DSenani\\x20Thunderheart\\x5B\\x2Furl\\x5D\\x5B\\x2Ficon\\x5D\\x5B\\x2Fli\\x5D\\x5Bli\\x5DNot\\x20sharable\\x5B\\x2Fli\\x5D\\x5Bli
';
$decoded = preg_replace('~\\\\x([A-Fa-f0-9]{2})~e', 'chr("0x$1")', $text);
Which gives you a string similar to this:
quest_start]Start: [item=16305][/icon][/li][li][icon name=quest_end]End: [url=/npc=12696]Senani Thunderheart[/url][/icon][/li][li]Not sharable[/li][li
(obviously, some kind of BB-code). To remove all bbcodes, yet one replacement is necessary:
$clean = preg_replace('~(\[.+?\])+~', ' ', $decoded);