preg_replace_callback(): Requires argument 2 to be a valid callback in - php

$ent_check = empty($modSettings['disableEntityCheck']) ?
array('preg_replace_callback(\'~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e\', \'$func[\\\'entity_fix\\\'](\\\'\\2\\\')\', ', ')') :
array('', '');
Warning: preg_replace_callback(): Requires argument 2, '$func['entity_fix']('\2')', to be a valid callback in...
I'm not quite sure what to do here. Any help from someone smarter than I would be greatly appreciated...

The First issue here is Intent. Well, the 2nd Argument passed to preg_replace_callback($arg1, $arg2...) is expected to be a Callable. That is why you have that error. It is unclear where you are going with your code but perhaps the code below could throw more light and help you either rethink/clarify your question, intent + goal or revisit your code. Consider this:
<?php
$string = "&#2510 whatever &#5870 again whatever &#7885";
$modSettings = array('disableEntityCheck'=>array());
$func = array(
"fix_stuff" => function($param=20){ echo $param;},
"do_stuff" => function($param=10){ echo $param;},
"entity_fix" => function($matches ){ return $matches[0] . "YES!!! ";},
);
$ent_check = empty($modSettings['disableEntityCheck']) ? array(preg_replace_callback('#\d#', $func['entity_fix'], $string )) : array('', '');
var_dump($ent_check);
// DISPLAYS
array (size=1)
0 => string '&#2YES!!! 5YES!!! 1YES!!! 0YES!!! whatever &#5YES!!! 8YES!!! 7YES!!! 0YES!!! again whatever &#7YES!!! 8YES!!! 8YES!!! 5YES!!! '
Notice that in the code above, the 2nd Argument passed to preg_replace_callback is a function though passed as a REFERENCE to the 'entity_fix' Key of the array: $func. This was intended to highlight the fact that it is also possible to pass the 2nd Argument in such a manner. It is hoped that this here gives you a little tip to kick off ;-)
Good Luck!!!

Related

"Warning: preg_replace_callback(): Requires argument 2, '&#x;', to be a valid callback" ; PHP Error

Setting up an App that should put Ads over images and I'm getting this WordPress error when running the code and it's been a frustrating week trying to fix this thing.
function cwali_json_encode_utf8callback($matches){
return html_entity_decode('&#x' . $matches[1] . ';', ENT_COMPAT, 'UTF-8');
}
function cwali_json_encode_utf8($json)
{
return preg_replace_callback('/\\\\u(\w{4})/', cwali_json_encode_utf8callback($matches), json_encode($json));
}
I expect it to work but it gives me the following error : Warning: preg_replace_callback(): Requires argument 2, '&#x;', to be a valid callback.
If someone could please come up with a solution, that would be amazing, thank you so much!
In your call
preg_replace_callback('/\\\\u(\w{4})/', cwali_json_encode_utf8callback($matches), json_encode($json));
instead of passing the callback function, you call the function, so its return value is passed as the argument. Try
preg_replace_callback('/\\\\u(\w{4})/', 'cwali_json_encode_utf8callback', json_encode($json));

variable sphinx filter with PHP

i m sorry for my english, i m french, i need your help for one thing please :
It s about filtering in php sphinx
this my code :
$filtres= array();
if(isset($_POST['Pharmacie']) and $_POST['Pharmacie'] ="1" ){ $filtres[]= 1;}
if(isset($_POST['Autres']) and $_POST['Autres'] ="8" ){ $filtres[] = 8;}
$varfiltres = 'array('.implode(" , ",$filtres).')';
if($filtres != array()){
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
}
i have error :
Warning: assert(): Assertion failed in \sphinxapi.php on line 850
in case only one variable isset (pharmacie or Autres) that work!
also if i do $sphinx->SetFilter('Type', array(1,8)) it work!
thanks for help.
SetFilter, takes an array directly. With:
$sphinx->SetFilter('Type', array(implode(",",$filtres)));
you are first converting the Array to a String, and then putting it in an new array. Don't do that :)
This is all that is needed:
$sphinx->SetFilter('Type', $filtres);

PHP JSON Google Definitions - accessing a value

EDIT#4: json_decode is failing and returning null on a seemingly valid json string. See below for more info
I am new to JSON/JSONP and I'm running into constant trouble accessing the values in the returned JSON with PHP. I have stripped the JSONP callback without issue using code I found on this board. I am getting a JSONP result from http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=love and struggling to access the first result for the meaning. It's a quite complex result, and I need to access the first meaning (in the node "text") from the below JSON result.
http://pastebin.com/hBTeBTUL
My best attempt was:
if (isset($json->primaries[1]->entries[1]->terms[1]->text))
The above was the best I could do, I just keep getting errors trying to return that text node saying it is undefined. I'd prefer to work with objects rather than associative arrays too, if possible, so please avoid telling me to set it to return assoc array.
Any help would be greatly appreciated. I'm really stuck :P
EDIT:
$json->primaries[1]->entries[1]->terms[0]->text didn't seem to work either. Here is the complete script. Ignore the $params array as it is not used, was going to use it to generate the query.
The script has been edited since when I first posted, I had an invalid JSON object, but the error seems to be fixed as it will now parse through JSON formatters.
The error i'm getting trying to print the value out is
PHP Notice: Trying to get property of non-object in /home/outil2/Plugins/GDefine.php on line 23
EDIT#2: added json_decode which was in my original solution, but got lost in the second version
<?php
class GDefine extends Plugin {
public static $enabled = TRUE;
public function onReceivedData($data) {
if ($data["message"][0] == ".def") {
$params = array (
"callback" => "a",
"sl" => "en",
"tl" => "en",
"q" => $data["message"][1]
);
$jsonp = file_get_contents(
"http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=" . $data["message"][1]);
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
var_dump($json);
print_r($json->primaries[1]->entries[1]->terms[0]->text);
if (isset($json->primaries[1]->entries[1]->terms[0]->text)) {
$text = $this->bold("Google Definition: ");
$text .= $this->teal($json->primaries[1]->entries[1]->terms[0]->text);
$this->privmsg($data["target"], $text);
} else {
$this->privmsg($data["target"], "error error error");
}
}
}
}
EDIT #3: this is the string I'm trying to json_decode, after using substr to remove the callback function, but am getting a NULL value returned on var_dump($json)
{"query":"love","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"love","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/lÉv/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"loves","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An intense feeling of deep affection","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"babies fill parents with intense feelings of \x3cem\x3elove\x3c/em\x3e","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"their \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e their country","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A deep romantic or sexual attachment to someone","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"it was \x3cem\x3elove\x3c/em\x3e at first sight","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"they were both \x3cb\x3ein \x3cem\x3elove\x3c/em\x3e with\x3c/b\x3e her","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"we were slowly \x3cb\x3efalling in \x3cem\x3elove\x3c/em\x3e\x3c/b\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A personified figure of \x3cem\x3elove\x3c/em\x3e, often represented as Cupid","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A great interest and pleasure in something","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"his \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e football","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"we share a \x3cb\x3e\x3cem\x3elove\x3c/em\x3e of\x3c/b\x3e music","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Affectionate greetings conveyed to someone on one\x27s behalf","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A formula for ending an affectionate letter","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"take care, lots of \x3cem\x3elove\x3c/em\x3e, Judy","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A person or thing that one \x3cem\x3eloves\x3c/em\x3e","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"she was \x3cb\x3ethe \x3cem\x3elove\x3c/em\x3e of his life\x3c/b\x3e","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"their two great \x3cem\x3eloves\x3c/em\x3e are tobacco and whiskey","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A friendly form of address","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"it\x27s all right, \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Used to express affectionate approval for someone","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"don\x27t fret, there\x27s a \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"(in tennis, squash, and some other sports) A score of zero; nil","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"\x3cem\x3elove\x3c/em\x3e fifteen","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"he was down two sets to \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]}]},{"type":"headword","terms":[{"type":"text","text":"love","language":"en","labels":[{"text":"Verb","title":"Part-of-speech"}]},{"type":"phonetic","text":"/lÉv/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"loved","language":"und","labels":[{"text":"past participle"}]},{"type":"text","text":"loves","language":"und","labels":[{"text":"3rd person singular present"}]},{"type":"text","text":"loving","language":"und","labels":[{"text":"present participle"}]},{"type":"text","text":"loved","language":"und","labels":[{"text":"past tense"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Feel a deep romantic or sexual attachment to (someone)","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"do you \x3cem\x3elove\x3c/em\x3e me?","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Like very much; find pleasure in","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"I\x27d \x3cem\x3elove\x3c/em\x3e a cup of tea, thanks","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"I just \x3cem\x3elove\x3c/em\x3e dancing","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"a fun-\x3cem\x3eloving\x3c/em\x3e girl","language":"en"}]}]}]}]}
I json_decode that and it returns NULL :(
You're trying to access an object that doesn't exist. Your code:
if (isset($json->primaries[1]->entries[1]->terms[1]->text)) // Doesn't exist
There's no terms[1] in entries[1] in primaries[1]. There's just 1 item; terms[0]. I think this will work for example:
if (isset($json->primaries[1]->entries[1]->terms[0]->text))
The first item in the array is indexed by 0 not 1, maybe that's your mistake.
Edit:
You also need to decode the JSON, change:
$json = substr($jsonp, 2, strlen($jsonp)-12);
to:
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
Edit:
You need to escape some unescaped characters in the JSON as well. Add this to your code:
Change:
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
to:
$json = substr($jsonp, 2, strlen($jsonp) - 12);
$json = str_replace("\\", "\\\\", $json);
$json = json_decode($json);

PHP Parse error: syntax error, unexpected ''

At the risk of getting a down vote I am going to ask this question to see if anyone can help me. I have been staring at this for a while and I can't figure it out.
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) on line 130
function pdf($type=null){
//get default or create a type
$data = $this->storeSessionData(
array(),
'SalesComp',
$this->variables
);
$type = ($type)? $type : $data['type']; //this is line 130
$this->set('data', $this->report('store', 'year', 3, $type));
$this->set(
'districtTitle',
array('N' => 'North', 'S' => 'South')
);
$districts = $this->Store->find(
'list',
array(
'fields' => array('Store', 'District'),
'conditions' => array('NOT' => array('District'=> 'O')
)
)
);
$this->set('districts', $districts);
$supervisor = $this->Store->find(
'list',
array('fields' => array('Store','Supervisor'),
'conditions' => array('NOT' => array('District'=> 'O')
)
)
);
$this->set('supervisor', $supervisor);
$this->set(
'supervisors',
$this->Supervisor->find(
'list',
array('fields' => array('Supervisor','ShortName')
)
)
);
$title = ($type == 'sales')?
'Sales Comparison Report':'Fuel Comparison Report';
$this->set('title', $title);
$this->layout = 'pdf';
$this->render();
}
The error suggests mismatched quotes or brackets somewhere. Unfortunately, when this happens, the line number in the error message may be some totally unrelated line -- it's just the first place where the compiler notices that the syntax is no longer valid. The error is actually somewhere before the code snippet in the question, so it's impossible for me to pinpoint it. Syntax highlighting in code editors can help in finding the mismatch.
You can find all php tokens here:
http://php.net/manual/en/tokens.php
There's 2 things about the marked line:
PHP is a scripted language with a VERY complicated syntax in reality. It might look simple, but in comparison to C or the like it's very complicated, and only because there where so many small (and many times incoherent) changes made over the years, and some things can't be changed anymore without breaking backwards compatibility. One of those things is handling of parantheses - PHP does not handle these in a mathematical way, but treat them specially depending on context. This means you should get rid of them in the marked line (there is no need for them in the first place either way)
"Invisible" (i.e. UTF8) whitespaces - these are source for many "strange" problems, especially if you develop on a mac (press alt + space). Easiest way to fix them: Retype the line. And DON'T try copy & pasting it, because you will copy the whitespace as well.
Also I would change the line to
if (! $type) $type = $data['type'];
I hope you know which values evaluate falsy in php, because it's not only NULL (you can find a list here: http://php.net/manual/en/language.types.boolean.php )
On a sidenote: PHP was developed as a simple way to write templates, so maybe you might want to have a look at some compiled programming languages if you want to build complex logic (C for example, which is by far simpler than PHP, and I've been doing PHP for more than 6 years now)
Try replacing
$type = ($type)? $type : $data['type'];
With
$type = $type ? $type : $data['type'];
I doubt that it makes any difference, but maybe a space between the variable named $type and the question mark is needed. For more information about this, check the PHP docs on the ternary operator.

Dissapearing PHP Variables

I am creating a 3D Secure PHP Project. I am having a rather bizzare issue in that the "MD" code is going missing when re-submitting the Array of data
My code is as follows :
$paRes = $_REQUEST['PaRes'];
$md = $_REQUEST['MD'];
require "payment_method_3d.php";
x_load('cart','crypt','order','payment','tests');
/*
* For Debugging Purposes
* Only.
echo "The Value Of PaRes is : ";
echo $paRes;
*/
$soapClient = new SoapClient("https://www.secpay.com/java-bin/services/SECCardService?wsdl");
$params = array (
'mid' => '',
'vpn_pswd' => '',
'trans_id' => 'TRAN0095', // Transaction ID MUST match what was sent in payment_cc_new file
'md' => $md,
'paRes' => $paRes,
'options' => ''
);
It seems that the $_REQUEST['MD'] string seems to go missing AFTER the soap call. Although I am having difficulty print this out to the screen. The strange thing is the $paRes variable works without issue.
Any ideas why this would be the case?
Check your case. PHP array keys are case sensitive. From this little bit of code it looks as if the request variable may be 'md' instead of 'MD'.
Try $md = $_REQUEST['md'];
PHP array statements are case sensitive, so this should work:....
$md = $_REQUEST['md'];
Thanks for your responses guys.
What was happening was the include page was sitting in front of the request methods and causing issues loading the REQUEST methods to the page.

Categories