PHP alternative of Javascript's pop() method [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there any PHP method to do:
var filename = $_POST["filename"];
filename = filename.split(".").pop();
How can I do the same above thing in PHP?

Something like this, perhaps?
$filetype = array_pop(explode('.',$filename));
It will generate an Only variables should be passed by reference notice. If you want to get rid of that, you'd need:
$fileparts = explode('.',$filename);
$filetype = array_pop($fileparts);
However, the best way to get a file's extension is by using pathinfo():
$filetype = pathinfo($filename, PATHINFO_EXTENSION);

You can do unset($filename[count($filename) - 1]);. Which will remove the last element in the array.

You'd want to do this:
$filename = $_POST['filename'];
$file_arr = explode(".",$filename); // may need to escape '.' here, can't remember
$filename = array_pop($file_arr);

In PHP, you probably don't need to convert to an array and pop; get the position of the last character, then get the rest of the string. From http://davidwalsh.name/php-file-extension:
substr(strrchr($file_name,'.'),1);

Related

Importing PHP variables into Javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
This question may have already been answered, but how can I import variables from PHP into a segment of Javascript in HTML? I have the following PHP variables (these are parameters passed into a PHP script):
$user = $GET[user];
$pass = $GET[pass];
And I am trying to access these variables with the following references in my Javascript code:
var user = "<?=$user?>";
var pass = "<?=$pass?>";
However, when I check the values of these variables with console.log I get the following result:
user =
pass =
Where am I slipping up? It seems like I just can't read these variables from PHP.
Multiple bugs:
$user = $_GET['user'];
^----^----^--- missing
var user = "<?=$user?>";
^^^^^^^^^^---nasty and can break JS.
Never dump arbitrary text from PHP into a JS code block. One single JS metacharacter and you've introduced a syntax error and the entire JS block is killed. Always use json_encode():
var user = <?= json_encode($user) ?>;
Change:
$user = $GET[user];
$pass = $GET[pass];
To:
$user = $_GET['user'];
$pass = $_GET['password'];

Outputting an array in alphatabetical order [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wondering if I have the below:
var mycars = new Array();
mycars[0] = "Manbearpigs";
mycars[1] = "Cool";
mycars[2] = "Coolz";
mycars[3] = "Radical";
mycars[4] = "GiantCools";
What is the best way I could output it in alphabetical order in the HTML.
Eg:
Cool
Coolz
GiantCools
Manbearpigs
Just Sort it
var mycars = new Array();
mycars[0] = "Manbearpigs";
mycars[1] = "Cool";
mycars[2] = "Coolz";
mycars[3] = "Radical";
mycars[4] = "GiantCools";
mycars.sort();
Array Sort
You can use sort method
so your code would be mycars.sort();
Use can use array.sort() method to sort your array:
mycars.sort();
Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
You can use Underscore Library, which the best one for operating data in JavaScript

Get the value of a div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

Converting a file to array with FS [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a simple "en.txt"
"TITLE" => "Amazing title of my page"
"COPYRIGHT" => "Copyright my site"
"BLABLA" => "A amazing sentence"
And I would like convert this in array for nodeJS with FS.
Thanks for your help.
First, read the file data using the fs.readFile method. Once you have the file data in a variable, you can convert it into an array using the following regular expression:
var regex = /"([^"]+)"\s*=>\s*"([^"]+)/g;
var match, results = {};
while((match = regex.exec(fileData)) !== null){
results[match[1]] = match[2];
}
console.log(results); // contains js array of data
See Fiddle.

Why does the Javascript replace() function not do anything? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's my function:
CSS_Left = '450px';
CSS_Left.replace('px', '');
alert(CSS_Left);
It returns '450px'
Here's an example:
http://jsfiddle.net/D2Mgb/
I want it to replace 'px' with nothing.
In JavaScript, Strings are immutable.
All types except objects define immutable values. Specifically, strings are immutable (unlike in C for instance).
So, str.replace returns a new string. Try this instead:
CSS_Left = CSS_Left.replace('px', '');
Or if you don't want to overwrite the original string, just do it in the alert call
alert(CSS_Left.replace('px', ''));
It's worth noting that .replace can also take a regexp. Though it's not necessary in your case, you could do something like this to achieve the same result
CSS_Left = CSS_left.replace(/px$/, '');
Because .replace() return a new string. If you want to update the old string you should write:
CSS_Left = CSS_Left.replace('px', '');
I am not sure, but the function replace() creates a new variable.
Try this :
var CSS_Left = '450px';
CSS_Left = CSS_Left.replace('px', '');
alert(CSS_Left);
The output of the replace call needs to be reassigned to CSS_Left

Categories

Resources