I get a string on request, using guzzle
$import = new ServerInviteClient();
$response = $import->client->request('GET', 'cB2EDPJ9wr');
$items = $response->getBody()->getContents();
dd($items);
$id = 1234567
There are more than 1000+ words in this line and I need to find the given value $id
I tried to search through whereLike, but it didn't work out, due to the fact that this is not a string in the database, but a query
How do I find the values in this string?
-------Edit
Part of dd($item), I need to find $id = xqc in this line
<!DOCTYPE html><html class="tw-root--hover"><head><meta charset="utf-8"><title>Twitch</title><meta property="og:title" content="xQc - Twitch"/><meta pr
operty="og:video:secure_url" content="https://player.twitch.tv/?channel=xqc&player=facebook&autoplay=true&parent=meta.tag"/><meta property="
al:ios:url" content="twitch.tv/xqc"/><meta name="twitter:app:name:iphone" content="Twitch"/><meta name="twitter:app:id:iphone" content="id460177396"/><m
eta name="twitter:app:url:iphone" content="twitch.tv/xqc"/><meta property="og:type" content="video.other"/><meta property="og:video:height" content="378
"/><meta content="https://www.twitch.tv/xqc" property="og:url"/><meta name="title" content="xQc - Twitch"/><meta name="twitter:image" content="https://s
tatic-cdn.jtvnw.net/jtv_user_pictures/xqc-profile_image-9298dca608632101-300x300.jpeg"/><meta property="al:android:package" content="tv.twitch.android.a
pp"/><link rel="alternate" media="only screen and(max-width: 640px)" href="https://m.twitch.tv/xqc"/><meta name="twitter:description" content="{[][][][]
[][\][\]' \' '\\'][]\]\]\]\]\][]\[]\[\][[]\[]\;[]'; .';. \;'/\;]/\][\]\[][\[\][][\P][\P][\P][\P][\P][\P[]P[]\][\[][\]'[\
']['["/><meta property="og:image" content="https://static-cdn.jtvnw.net/jtv_user_pictures/xqc-profile_image-9298dca608632101-300x300.jpeg"/><met
a property="og:video" content="https://player.twitch.tv/?channel=xqc&player=facebook&autoplay=true&parent=meta.tag"/><meta property="og:vide
o:type" content="text/html"/><meta property="al:ios:app_name" content="Twitch"/><meta name="twitter:app:id:googleplay" content="tv.twitch.android.app"/>
<meta name="twitter:app:id:ipad" content="id460177396"/><meta name="robots" content="index"/><meta name="twitter:title" content="xQc - Twitch"/><meta pr
operty="fb:app_id" content="161273083968709"/><meta property="al:android:app_name" content="Twitch"/><meta name="twitter:app:name:googleplay" content="T
witch"/><meta property="og:site_name" content="Twitch"/><meta property="og:description" content="{[][][][][][\][\]' \' '\\'][]\]\]\]\]\]
[]\[]\[\][[]\[]\;[]'; .';. \;'/\;]/\][\]\[][\[\][][\P][\P][\P][\P][\P][\P[]P[]\][\[][\]'[\']['["/><meta name="twitter:card" cont
ent="summary"/><meta name="twitter:site" content="#twitch"/><meta property="og:video:width" content="620"/><meta content="id460177396" property="al:ios:
app_store_id"/><meta name="twitter:app:name:ipad" content="Twitch"/><meta name="twitter:app:url:ipad" content="twitch.tv/xqc"/><meta name="description"
content="{[][][][][][\][\]' \' '\\'][]\]\]\]\]\][]\[]\[\][[]\[]\;[]'; .';. \;'/\;]/\][\]\[][\[\][][\P][\P][\P][\P][\P][\P[]P
[]\][\[][\]'[\']['["/><link rel="canonical" href="https://www.twitch.tv/xqc"/>...
You could use the native php function strpos. This function returns the index of a substring in a string, or -1 if not found. You will need to validate that the returns value is not -1:
$id = 1234567
$import = new ServerInviteClient();
$response = $import->client->request('GET', 'cB2EDPJ9wr');
$items = $response->getBody()->getContents();
$found = strpos($items, $id) !== -1;
In php 8.0+, you could use the easier str_contains.
However, this is not the best approach. The id could be a found in another property of your items, and the results would be innacurate. A better approach is to decode the body of your response, and validate each items to see if it's id match.
You have not specified if the body is in JSON format, XML format or any other format, so we can't give you an exact answer, unfortunatly
Related
I am new to the PHP world and I have an XML file that looks like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<outageResponse>
<stormFlag>C</stormFlag>
<timeStamp>2015-06-22T16:10:32</timeStamp>
<stormCount />
<outage job="239369" circ="0587" start="2015-06-22T06:12:00" ert="2015-06-22T19:00:00" ertty="F" cau="We have determined that a combination of factors has caused a problem." cust="0000019" comm="605" x="-117.28778580262241" y="33.123002776709676" district="NC" substation="XYZ" device="DBE.279317" calltype="OUT" damage="" />
<outage job="239454" circ="0587" start="2015-06-22T06:01:44" ert="2015-06-22T19:00:00" ertty="F" cau="We have determined that a combination of factors has caused a problem." cust="0000016" comm="605" x="-117.28778580262241" y="33.123002776709676" district="NC" substation="ABC" device="LBE.283559" calltype="OUT" damage="" />
<outage job="239413" circ="220" start="2015-06-22T09:03:00" ert="2015-06-22T16:30:00" ertty="F" cau="Wprking on it." cust="0000007" comm="386" x="-116.67117489543362" y="33.110485186359256" district="RA" substation="DEF" device="<inline_jumper.4356>" calltype="PLAN" damage="" switchplan="71474" />
</outageResponse>
So, In ColdFusion, I would do the following:
<cfset MyXMLDoc = XMLParse(XMLURL)>
<cfset MyOutages = xmlSearch(MyXMLDoc,'/outageResponse/outage')>
<cfloop from="1" to="#arraylen(MyOutages)#" index="i">
<cfset MyJob = MyOutages[i].XmlAttributes["job"]>
<cfset MyCircuit = MyOutages[i].XmlAttributes["circ"]>
</cfloop>
I am unable to find examples of how I would do this in PHP.
There are many simple examples out there, but nothing for the above XML file.
I can figure out how to pull the values of the attributes as such:
echo "Value of job attribute in the first outage array: " . (string)$MyXMLDoc->outage[0]->attributes()->job;
However, I haven't been able to figure out how many arrays of outage I have in the XML file since each time I get the XML file, there may be different number of outages or none at all.
Part of the problem is that the XML is invalid. Proper XML must be escaped for "<" and "&", except in certain contexts. So, if you take out the "<>" (or better yet, convert them to < and > inside the quotes on line 8 of the XML, we can use SimpleXML to parse this.
$filedata = 'Your XML from a file, etc.';
$xmlData = new SimpleXMLElement($fileData);
echo "Children: " . $xmlData->count() . "\n";
$outageChildren = 0;
foreach ($xmlData as $outage) {
if (empty($outage)) continue; // Because of mixed list
$outageChildren++;
$job = $outage['job'];
$circuit = $outage['circ'];
echo "Value of job attributes '$job' and circuit '$circuit'\n";
}
echo "Total 'outage' children: $outageChildren\n";
Output:
Children: 6
Value of job attributes '239369' and circuit '0587'
Value of job attributes '239454' and circuit '0587'
Value of job attributes '239413' and circuit '220'
Total 'outage' children: 3
It is more typical in XML to put a list (or array) of items in their own container, like so:
<outageResponse>
<stormFlag>C</stormFlag>
<timeStamp>2015-06-22T16:10:32</timeStamp>
<stormCount />
<outages>
<outage job="239369" circ="0587" start="2015-06-22T06:12:00" ert="2015-06-22T19:00:00" ertty="F" cau="We have determined that a combination of factors has caused a problem." cust="0000019" comm="605" x="-117.28778580262241" y="33.123002776709676" district="NC" substation="XYZ" device="DBE.279317" calltype="OUT" damage="" />
<outage job="239454" circ="0587" start="2015-06-22T06:01:44" ert="2015-06-22T19:00:00" ertty="F" cau="We have determined that a combination of factors has caused a problem." cust="0000016" comm="605" x="-117.28778580262241" y="33.123002776709676" district="NC" substation="ABC" device="LBE.283559" calltype="OUT" damage="" />
<outage job="239413" circ="220" start="2015-06-22T09:03:00" ert="2015-06-22T16:30:00" ertty="F" cau="Wprking on it." cust="0000007" comm="386" x="-116.67117489543362" y="33.110485186359256" district="RA" substation="DEF" device="<inline_jumper.4356>" calltype="PLAN" damage="" switchplan="71474" />
</outages>
</outageResponse>
It isn't required, but it's helpful. Assuming you can't change the input format, you'll need to keep the if...continue on the first line of the foreach loop to skip entries that are not an "outage". Note that SimpleXML actually uses the name of the variable in the foreach ($outage) to determine which elements to read in. That's a little strange, but it's why the "stormFlag", "timeStamp" and "stormCount" elements are not counted (or more specifically are skipped) by our foreach loop.
Note: As you alluded to in your question, remember to cast these values with (string) before using them as a string since you will sometimes get weird results when the rest of your code expects it to be a string and it isn't quite (it's still a member of SimpleXMLElement).
I am trying to get an Access DB converted into MySQL. Everything works perfectly, expect for one big monkey wrench... If the access db has any non standard characters, it wont work. My query will tell me:
Incorrect string value: '\xE9d'
If I directly echo out the rows text that has the 'invalid' character I get a question mark in a black square in my browser (so é would turn into that invalid symbal on echo).
NOTE: That same from will accept, save and display the "é" fine in a textbox that is used to title this db upload. Also if I 'save as' the page and re-open it up the 'é' is displayed correctly....
Here is how I connect:
$conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=$fileLocation;SystemDB=$securefilePath;Uid=developer;Pwd=pass;charset=utf;");
I have tried numerous things, including:
$conn -> exec("set names utf8");
When I try a 'CurrentDb.CollatingOrder' in access it tells me 1033 apparently that is dbSortGeneral for "English, German, French, and Portuguese collating order".
What is wrong? It is almost like the PDO is sending me a collation my browser and PHP does not fully understand.
The Problem
When using native PHP ODBC features (PDO_ODBC or the older odbc_ functions) and the Access ODBC driver, text is not UTF-8 encoded, even though it is stored in the Access database as Unicode characters. So, for a sample table named "Teams"
Team
-----------------------
Boston Bruins
Canadiens de Montréal
Федерация хоккея России
the code
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Access character test</title>
</head>
<body>
<?php
$connStr =
'odbc:' .
'Driver={Microsoft Access Driver (*.mdb)};' .
'Dbq=C:\\Users\\Public\\__SO\\28311687.mdb;' .
'Uid=Admin;';
$db = new PDO($connStr);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT Team FROM Teams";
foreach ($db->query($sql) as $row) {
$s = $row["Team"];
echo $s . "<br/>\n";
}
?>
</body>
</html>
displays this in the browser
Boston Bruins
Canadiens de Montr�al
????????? ?????? ??????
The Easy but Incomplete Fixes
The text returned by Access ODBC actually matches the Windows-1252 character encoding for the characters in that character set, so simply changing the line
$s = $row["Team"];
to
$s = utf8_encode($row["Team"]);
will allow the second entry to be displayed correctly
Boston Bruins
Canadiens de Montréal
????????? ?????? ??????
but the utf8_encode() function converts from ISO-8859-1, not Windows-1252, so some characters (notably the Euro symbol '€') will disappear. A better solution would be to use
$s = mb_convert_encoding($row["Team"], "UTF-8", "Windows-1252");
but that still wouldn't solve the problem with the third entry in our sample table.
The Complete Fix
For full UTF-8 support we need to use COM with ADODB Connection and Recordset objects like so
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Access character test</title>
</head>
<body>
<?php
$connStr =
'Driver={Microsoft Access Driver (*.mdb)};' .
'Dbq=C:\\Users\\Public\\__SO\\28311687.mdb';
$con = new COM("ADODB.Connection", NULL, CP_UTF8); // specify UTF-8 code page
$con->Open($connStr);
$rst = new COM("ADODB.Recordset");
$sql = "SELECT Team FROM Teams";
$rst->Open($sql, $con, 3, 3); // adOpenStatic, adLockOptimistic
while (!$rst->EOF) {
$s = $rst->Fields("Team");
echo $s . "<br/>\n";
$rst->MoveNext;
}
$rst->Close();
$con->Close();
?>
</body>
</html>
A bit more easily to manipulate the data. (Matrix array).
function consulta($sql) {
$db_path = $_SERVER["DOCUMENT_ROOT"] . '/database/Registros.accdb';
$conn = new COM('ADODB.Connection', NULL, CP_UTF8) or exit('Falha ao iniciar o ADO (objeto COM).');
$conn->Open("Persist Security Info=False;Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=ifpb#10510211298;Data Source=$db_path");
$rs = $conn->Execute($sql);
$numRegistos = $rs->Fields->Count;
$index = 0;
while (!$rs->EOF){
for ($n = 0; $n < $numRegistos; $n++) {
if(is_null($rs->Fields[$n]->Value)) continue;
$resultados[$index][$rs->Fields[$n]->Name] = $rs->Fields[$n]->Value;
echo '.';
}
echo '<br>';
$index = $index + 1;
$rs->MoveNext();
}
$conn->Close();
return $resultados;
}
$dados = consulta("select * from campus");
var_dump($dados);
Found the following solution. True, I did not have the opportunity to test it on php. But I suppose it should work out.
In order for native PHP ODBC features (PDO_ODBC or the older odbc_ functions) and the Access ODBC driver to be able to correctly subtract texts in Unicode encoding, that stored in the Access database as Unicode character, it is need enables "Beta: Use Unicode UTF-8 for worldwide language support" in Region Settiongs of Windows Operetion System.
After I did this at me, many programs using the standard ODBC driver MC Access, began to display correct texts in Unicode encoding.
All Settings -> Time & Language -> Language -> "Administrative Language Settings"
I was looking at the internal representation of PHP session file and I noticed that the session keys are separated by the pipe character |.
Before getting into the problem I encountered, let me give a quick tutorial on how the session file is formatted. At least, this is how it was formatted on my Mac (10.9.4, PHP 5.4.24).
Session File Format
Say I have the following code:
$_SESSION["age"] = 26;
$_SESSION["car"] = "Mazda";
$_SESSION["nerdy"] = true;
$_SESSION["likes"] = array(42, "being meta");
$_SESSION["stats"] = array("bmi" => 1000);
Then it gets stored in the session variable like this:
age|i:26;car|s:5:"Mazda";nerdy|b:1;
likes|a:2:{i:1;i:42;i:2;s:10:"being meta"}
stats|a:1:{s:3:"bmi";i:1000}
The general format is
session_key|session_value[;session_key|value] etc.
where session_value is of the general form
type[:size]:value
More specifically (if anyone's interested),
strings: s:3:"some text"
integers: i:4
booleans: b:1 (true) or b:0 (false)
arrays: a:2:{session_value;session_value;session_value;session_value}
where the four session_values in the array of size 2 are the key;value key;value pairs.
The Problem
You can see that in the above, the top-level session keys are separated by the | character. But what if one of our session key names includes the | character?
Well, I tried it. And when I did, the entire session file (in /tmp) was blank (and the variables were definitely not set). Is this an oversight by the PHP devs or an undocumented limitation (or is it documented somewhere)?
This could be easily solved by putting the $_SESSION keys themselves in quotes or backslashing any pipe in a $_SESSION key string. This isn't a big problem for me personally, since I can't fathom why I would need to put a | in a $_SESSION variable key - just curious about it.
Its a known bug
https://bugs.php.net/bug.php?id=33786
The work around is update to 5.5.4 and use the php_serialize session serializer
Using this php file 'test3.php', I demonstrated that both | (pipe) and ! (bang) will cause _SESSION to fail in PHP 5.4 if they are used in a _SESSION key. Also, no other ASCII characters between 0x20 and 0x7f cause it to fail. I don't have easy access to other versions of PHP to empirically check their behavior.
<?php
session_start( );
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test session keys</title>
</head>
<body>
<form method="post" target="test3.php">
<input type="submit" id="submit" name="submit" value="Submit">
<?php
echo "<br>_SESSION...<br>";
var_dump( $_SESSION );
echo "\n".'<br><input type="text" id="text" name="text" length="1" ';
if( $_SERVER[ 'REQUEST_METHOD' ] != 'POST' ) {
$t = chr( 32 );
echo 'value="&#'.ord( $t ).';">';
$_SESSION[ 'key' ] = ' ';
$str = 'first';
} else {
$str = 'good';
if( count( $_SESSION ) != 2 ) {
$str = 'BAD';
}
$_SESSION = array( );
$t = substr( $_POST[ 'text' ], 0, 1);
echo 'value="&#'.(ord($t) + 1 ).';">';
$_SESSION[ 'key' ] = chr(ord($t) + 1 );
}
echo "\n".'<br><input type="text" id="check" name="check" length="1" value="&#'.ord( $t ).';">';
echo "\n".'<br><input type="text" id="check2" name="check2" length="6" value="%'.bin2hex( $t ).'";">';
echo '<span id="success"></span>';
echo "<script> document.getElementById( 'success' ).innerHTML = '".$str."'; </script>";
$_SESSION[ "alpha".$_SESSION['key']."four" ] = 'Hello World';
?>
</form>
</body>
</html>
I am new to PHP and I am getting an error:
"Fatal error: Cannot use object of type Confrence as array on line 22"
I am trying to take data that is put into an array into a table where you have the number 1 seed in column 1 row 1 and the number 16 seed in column 2 row 1. I do not know if this is the correct logic to go about this, but that is my goal. For some reason it won't echo the first index of the array. This is my code.
Class Conference:
<?php
class Confrence
{
public $team1;
public $team2;
function loadGame($teamone, $teamtwo)
{
$this->$team1 = $teamone;
$this->$team2 = $teamtwo;
}
}
?>
This is my main code.
<?php print( '<?xml version = "1.0" encoding = "utf-8"?>' ) ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>User Selection Page</title>
</head>
<?php
require_once("loadGameClass") or die ("Could not load file");
$westTeams = array();
$loadGameClass = new Confrence();
$loadGameClass->loadGame("(1) Gonzaga", "(16) Southern U");
$westTeams = $loadGameClass;
echo $westTeams[0];
?>
<body>
</body>
</html>
The variables for $westTeams and $loadGameClass reference an your Conference object, which is not an array. Therefore, you cannot use notation like $westTeams[0].
If you're planning on populating the class with many teams, you might want to submit an array to the Conference class and have that stored.
Class Conference:
class Conference
{
protected $teams;
function loadTeams($teams)
{
$this->teams = $teams;
}
function getTeams()
{
return $this->teams;
}
}
Regarding putting these in a table, you could use the following code. I'm assuming you have a total of 16 teams.
<table>
<tbody>
<?php
/* Load your teams in the array */
$loadGameClass = new Conference();
$loadGameClass->loadTeams(array(...));
$teams = $loadGameClass->getTeams();
for ($i = 0; $i < 8; $i++) {
$highSeed = $teams[$i];
$lowSeed = $teams[((2*8)-1)-$i];
echo "<tr><td>$highSeed</td><td>$lowSeed</td></tr>";
}
?>
</tbody>
</table>
This will output the first seed in the first column, first row and the 16th seed in the 2nd column, first row. The pattern will repeat down to the 8th column.
In my code, the conference class is somewhat useless, as it's just a container for the teams array, but you can add functionality to that class to make it worthwhile.
The loadGame method isn't returning anything, so using echo on it all won't do anything.
The error you see is due to the [x] syntax being for arrays only, you're using a class.
In the case the class member vars are public so you can use the following in your html:
$westTeams->team1;
But you also need to change the class, you have a syntax error:
$this->$team1 should be $this->team1
istead of
$westTeams[0]
use
$westTeams->team1
Also use
$this->team1
instead of
$this->$team1
Getting an Undefined offset: 1 error when try the following code that outputs some data to word document...
Using CodeIgniter in the controller I have:
$htmltodoc= new HTML_TO_DOC();
$htmltodoc->createDoc("<h1>Testing data </h1>", "testfile.doc");
In the helper directory I am using the following functions in a class called HTML_TO_DOC below...
class HTML_TO_DOC
{
var $docFile="";
var $title="";
var $htmlHead="";
var $htmlBody="";
/**
* Constructor
*
* #return void
*/
function HTML_TO_DOC()
{
$this->title="Untitled Document";
$this->htmlHead="";
$this->htmlBody="";
}
/**
* Set the document file name
*
* #param String $docfile
*/
function setDocFileName($docfile)
{
$this->docFile=$docfile;
if(!preg_match("/\.doc$/i",$this->docFile))
$this->docFile.=".doc";
return;
}
function setTitle($title)
{
$this->title=$title;
}
/**
* Return header of MS Doc
*
* #return String
*/
function getHeader()
{
$return = <<<EOH
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 9">
<meta name=Originator content="Microsoft Word 9">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<title>$this->title</title>
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:DoNotHyphenateCaps/>
<w:PunctuationKerning/>
<w:DrawingGridHorizontalSpacing>9.35 pt</w:DrawingGridHorizontalSpacing>
<w:DrawingGridVerticalSpacing>9.35 pt</w:DrawingGridVerticalSpacing>
</w:WordDocument>
</xml><![endif]-->
<style>
<!--
/* Font Definitions */
#font-face
{font-family:Verdana;
panose-1:2 11 6 4 3 5 4 4 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:536871559 0 0 0 415 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0in;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:7.5pt;
mso-bidi-font-size:8.0pt;
font-family:"Verdana";
mso-fareast-font-family:"Verdana";}
p.small
{mso-style-parent:"";
margin:0in;
margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:1.0pt;
mso-bidi-font-size:1.0pt;
font-family:"Verdana";
mso-fareast-font-family:"Verdana";}
#page Section1
{size:8.5in 11.0in;
margin:1.0in 1.25in 1.0in 1.25in;
mso-header-margin:.5in;
mso-footer-margin:.5in;
mso-paper-source:0;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1032">
<o:colormenu v:ext="edit" strokecolor="none"/>
</o:shapedefaults></xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1"/>
</o:shapelayout></xml><![endif]-->
$this->htmlHead
</head>
<body>
EOH;
return $return;
}
/**
* Return Document footer
*
* #return String
*/
function getFotter()
{
return "</body></html>";
}
/**
* Create The MS Word Document from given HTML
*
* #param String $html :: URL Name like http://www.example.com
* #param String $file :: Document File Name
* #param Boolean $download :: Wheather to download the file or save the file
* #return boolean
*/
function createDocFromURL($url,$file,$download=false)
{
if(!preg_match("/^http:/",$url))
$url="http://".$url;
$html=#file_get_contents($url);
return $this->createDoc($html,$file,$download);
}
/**
* Create The MS Word Document from given HTML
*
* #param String $html :: HTML Content or HTML File Name like path/to/html/file.html
* #param String $file :: Document File Name
* #param Boolean $download :: Wheather to download the file or save the file
* #return boolean
*/
function createDoc($html,$file,$download=false)
{
if(is_file($html))
$html=#file_get_contents($html);
$this->_parseHtml($html);
$this->setDocFileName($file);
$doc=$this->getHeader();
$doc.=$this->htmlBody;
$doc.=$this->getFotter();
if($download)
{
#header("Cache-Control: ");// leave blank to avoid IE errors
#header("Pragma: ");// leave blank to avoid IE errors
#header("Content-type: application/octet-stream");
#header("Content-Disposition: attachment; filename=\"$this->docFile\"");
echo $doc;
return true;
}
else
{
return $this->write_file($this->docFile,$doc);
}
}
/**
* Parse the html and remove <head></head> part if present into html
*/
function _parseHtml($html)
{
$html=preg_replace("/<!DOCTYPE((.|\n)*?)>/ims","",$html);
$html=preg_replace("/<script((.|\n)*?)>((.|\n)*?)<\/script>/ims","",$html);
preg_match("/<head>((.|\n)*?)<\/head>/ims",$html,$matches);
$head=$matches[1];
preg_match("/<title>((.|\n)*?)<\/title>/ims",$head,$matches);
$this->title = $matches[1];
$html=preg_replace("/<head>((.|\n)*?)<\/head>/ims","",$html);
$head=preg_replace("/<title>((.|\n)*?)<\/title>/ims","",$head);
$head=preg_replace("/<\/?head>/ims","",$head);
$html=preg_replace("/<\/?body((.|\n)*?)>/ims","",$html);
$this->htmlHead=$head;
$this->htmlBody=$html;
return;
}
/**
* Write the content int file
*
* #param String $file :: File name to be save
* #param String $content :: Content to be write
* #param [Optional] String $mode :: Write Mode
* #return void
* #access boolean True on success else false
*/
function write_file($file,$content,$mode="w")
{
$fp=#fopen($file,$mode);
if(!is_resource($fp))
return false;
fwrite($fp,$content);
fclose($fp);
return true;
}
}
It will be great if someone can please help me solve this error.. thanks alot..
PS
Undefined offset: 1
means you are trying to read an array key '1' when there is none. That would probably mean the line you are getting it is here:
$head=$matches[1];
or
$this->title = $matches[1];
So your matches do not contain a [1] element, so you do not 'hit' anything there.
Debugging howto:
remove everything after $head=$matches[1];
Is the problem still there: you've found your line. Is it not? It's the second line.
For the preg_match that is the problem, echo the $html (or $head) vars to see their contents. Mind you, the will not show up in a browser if they are html, so check the source or do this on the commandline.
Try to figure out if your var has the content you expect, and if you could actually match with your regular expression. If not: fix the expression or the var. if so:
Make a test-file with the contents of the var hardcoded in your preg_match. Is the problem still there? maybe var_dump your $matches to be sure
If problem still there (you've probably hit the problem by now, but nevertheless): post the test code (that last testfile with the hardcoded preg_match that troublesome) and ask where you are getting it wrong.
bottomline: post a MINIMAL piece of code where it still goes wrong. You're bound to find the problem on the way of making that minimal problem, but if you do not it's a lot easier for us to help you.
Nowhere in your code are you actually checking that you managed to successfully open the document to parse, also can you show us more of the code nor do you even check to see if $matches has any content. First thing I suggest you do is to format your code so it is easier to read then look at some if statements to make sure that things are being set.
Just the second tip alone will save you countless hours in the future.
Also remember, arrays start at [0] so if you know that matches is only going to have one thing stored in it, try changing the $matches[1] to $matches[0]
I'm going to go ahead and say that your issue is that you don't have good error messages, not that you've an undefined index.
Follow the track to get to that point:
$head=$matches[1]; is an undefined index.
$matches is retrieved by looking for the head of some HTML which is being parsed with a regexp (*HUGE* No-no by the way. You should really look into a legit HTML parser)
That happens because there is a problem with whatever calls _parseHtml.
_parseHtml is called by createDoc.
You're passing in "<h1>Testing data </h1>" as HTML.
All of that given, OF COURSE you are getting no index found, you're not passing in valid HTML, so it is unable to convert it. Try "<html><head></head><body><h1>Testing data </h1></body></html>" (note: your class does not accept <head /> as a valid head tag... that's a problem).
And there are other areas where this class can be improved.
You're using PHP4 style OOP. Unless PHP4 is part of the functional spec. this is a big no-no. You want _parseHtml to be private...
At one point I noticed this:
EOH;
return $return;`
The probem is that Heredoc does not allow for indented closings. It's in big, red letters. I think they mean it.
createDocFromURL does not allow for https
$doc.=$this->htmlBody; (in createDoc) is not consistent with the code immediately preceding and following it -- everything works with get<value> and that line is a direct access of a property.
You have a fotter in your class (a typo in getFotter)
If PHP 5 is an option, you should use file_put_contents in write_file.
You have inconsistent naming of methods -- write_file v.s. getHeader
I'm not trying to knock you down, but most of these have a large potential for causing problems in the future. You really should consider them.