Can somebody please explain to me where can PHP script get this functions from, I have a PHP script that has this few lines:
<?php
$args1 = array();
$gethosts = get_xml_host_objects($args1); //grabbing internal xml data from backend
$args2 = array();
$gethoststatus = get_xml_host_status($args2);
$args3 = array();
$getparenthosts = get_xml_host_parents($args3);
So I do not understand from where and how could this PHP script be referencing those functions, could somebody just give me few examples suggestions of where to look?
Maybe, there is a file that includes the two scripts. Somethings like that
<?php
include 'file_with_function.php';
include 'your_file.php';
?>
These lines can help. Place them at the beginning of your file
echo '<pre>';
debug_print_backtrace();
echo '</pre>';
It looks like those were self defined functions, maybe you can try grep thru entire source tree to see if which .php file define these functions and include it in this script.
Related
I have a javascript function receiving php arrays like this :
let options = <?php echo $arrayoptions; ?>;
let optlibs = <?php echo $arrayoptlib; ?>;
let forms = <?php echo $arrayforms; ?>;
It works well but problem arises when I try to minify function. All minifiers I tried gave en error with php part. What can I do ?
Yes, you will run into errors because PHP is not part of Javascript syntax.
Even your Javascript part for the arrays seems dynamically generated by PHP.
This will be impossible to handle.
The only way to work around this would be modify your javascript to fetch those arrays and store them somewhere your whole script can access them for later use.
Then, the script can be minified without issue since PHP syntax are no longer part of it.
I write the following script that creates a nice JSON of all the images under the current folder:
<?php
header('Content-type: application/json');
$output = new stdClass();
$pattern="/^.*\.(jpg|jpeg|png|gif)$/i"; //valid image extensions
$dirs = array_filter(glob('*'), 'is_dir');
foreach ($dirs as $dirname) {
$files = glob(''.$dirname.'/*');
$images = preg_grep($pattern, $files);
$output->{$dirname} = $images;
}
echo json_encode($output, JSON_PRETTY_PRINT);
?>
I have an HTML file with a basic page and I want to display the JSON's data in a formatted way after some javascript manipulation.
So the question is how can I get the PHP data into a javascript variable?
<html>
...
<body>
<script src="images.php"></script>
<script type="text/javascript">
// Desired: Get access to JSON $output
</script>
...
<div>
<img ... >
</div>
</body>
</html>
I tried to put both https://stackoverflow.com/a/61212271/1692261
and https://stackoverflow.com/a/50801851/1692261 inside that script tag but none of them work so I am guessing I am missing something fundamental here (my ever first experience with PHP :)
you should focus on what needs to be done, but currently you are trying to implement your own idea. maybe you should change your approach and do what you want in another way?
passing php variable to js is possible. but for what reason do you need this json? if you want to operate with it to generate html (f.e show images to user) you can do it on pure php without js. if you need exactly json you can generate json file with php and and get this file via additional js request. but the simplest way is
// below php code that generates json with images
$images = json_encode($output, JSON_PRETTY_PRINT);
...
// php code but in html template
<script type="text/javascript">
var images = "<?= $images ?>";
</script>
I won't guarantee that this js line is going to work but you get the idea)
P.S you dont need to use stdClass for such purposes. we do it via arrays (in you case it will be associative arrays), arrays are very powerful in php. json_encode() will generate same json from both array or object. but if this part of code works fine that let it stay as it is
I took #Zeusarm advice and just used ajax (and jquery) instead.
For others need a reference:
Nothing to change in PHP script in the original post.
Add jquery to the HTML file with <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Make a GET request like this:
<script type="text/javascript">
var images = ''
$.get('images.php',function (jsondata) {
images = jsondata
});
I am sure this is not the cleanest code but it works :)
I had to change my question to simple question
For example: my XML file name is: ABC.xml
inside of this xml file is:
<li class="thumb"><a class="Alibaba" href="google.com"></a></li>
So, here is my question:
How to use PHP to search string inside of ABC.xml and find Alibaba name and replace it to AlibabaColor name and save it to ABC.xml
Please guide me and give me an example for this. Thank you
You can do this using php str_replace among many other functions. Here is a couple examples ->
If the XML is in a string already you can do this
PHP:
<?php
$myXmlString = str_replace('Alibaba', 'AlibabaColor', $myXmlString);
?>
If you need to get the XML data from another file there are a few ways to to do it depending on if you need to save it, just replace it and display it, or really what you are doing. Some of this comes down to preference.
Just need to display it?
<?php
$xml = file_get_contents(/path/to/file); // or http://path.to/file.xml
$myXmlString = str_replace('Alibaba', 'AlibabaColor', $xml);
?>
Need to change the file itself?
<?php
$xml = file_get_contents(/path/to/file); // or http://path.to/file.xml
$myXmlString = str_replace('Alibaba', 'AlibabaColor', $xml);
file_put_contents(/path/to/file, $myXmlString);
?>
--
To answer the below comment, here is working code from my server ->
xml:
<li>hello</li>
php:
$file = '/home/username/public_html/xml.xml';
$xml = file_get_contents($file);
$text = str_replace('hello','world',$xml);
file_put_contents($file, $text);
xml.xml after:
<li>world</li>
Keep in mind to change 'username' to the right one, or use an FQDN if you are more comfortable doing so. Make sure that the file has permission to be written to by a script as well.
Hi have looking on various questions but none of them seem to help me. I have a php variable in my php code and I am trying to access that in my javascript when I do. . .
var thing = "<?php echo($phpvariable); ?>";
then when I do
alert(thing);
It comes out to be "<?php echo($phpvariable); ?>" in the alert statement
What am I doing wrong?
Your PHP is obviously not being parsed. Are you in a .php file? If you're in a .js file, you'll need the server to parse those (or, more safely, put the PHP part somewhere in the DOM that the JS can access)
However, you're doing it wrong:
var thing = <?php echo json_encode($phpvariable); ?>;
Note: no quotes. json_encode will take care of that for you.
If this code is in a function in javascirpt that executes on click or at a specific event, then:
You are writing PHP Syntax in javascript, there is no way that you load the page then you run the php code. PHP code runs on the server side, so before any other HTML Javascript code executes
Else if you want to dynamically set the variable thing in javascript when the page is first loaded, then most probably you meant to write in the php file:
var thing = <?php echo '"'.$phpvariable.'"'; ?>;
I need to populate a field with the email address of the person that logged in. Since this app is used by various clients, I want to add the code as needed to the external javascript code. I have been working with json, but now i wonder if this is the answer.
In my php code I have this:
<?php
$myemail = $this->session->userdata('USER_EMAIL');
//echo $myemail;
next I have this:
var $jsonEmail = trim(json_encode($myemail));
Then in my js custom page I have this:
var jsonObj = $jsonEmail;
document.getElementById("email-12").innerHTML=jsonObj.value;
It's not working, but since I am new to this, I don't know what I am doing wrong. Any help would be appreciated.
You're mixing PHP and JavaScript which doesn't work. Your PHP should look like this:
$jsonEmail = trim(json_encode($myemail)); // Get rid of 'var'
Your JavaScript should look like this:
var jsonObj = <?php echo $jsonEmail; ?>
You could do something like this, It will be dirty, but I think will work.
var jsonObj = <?php echo $jsonEmail ?>;
document.getElementById("email-12").innerHTML=jsonObj.value;
You can't mix serverside PHP with localside Javascript. PHP write text to SDIO, and the browser interpret it like things, like JS code, or HTML.
You will need to echo your $jsonEmail variable to your JavaScript, eg:
var jsonObj = <?php echo $jsonEmail; ?>
document.getElementById("email-12").innerHTML=jsonObj.value;