phpjs

Javascript equivalent of PHP's list()

烈酒焚心 提交于 2020-01-18 04:57:28
问题 Really like that function. $matches = array('12', 'watt'); list($value, $unit) = $matches; Is there a Javascript equivalent of that? 回答1: There is, in 'newer' versions of Javascript: Destructuring assignment - Javascript 1.7. It's probably only supported in Mozilla-based browsers, and maybe in Rhino. var a = 1; var b = 3; [a, b] = [b, a]; EDIT: actually it wouldn't surprise me if the V8 Javascript library (and thus Chrome) supports this. But don't count on it either Now supported in all

array_count_values for JavaScript instead

假装没事ソ 提交于 2019-12-29 01:22:23
问题 I have the following PHP-script, now I need to do the same thing in JavaScript. Is there a function in JavaScript that works similar to the PHP function, I have been searching for days but cannot find anything similar? What I want to do is to count the number of times a certain word is being used in an array. $interfaceA = array($interfaceA_1,$interfaceA_2,$interfaceA_3,$interfaceA_4,$interfaceA_5,$interfaceA_6,$interfaceA_7,$interfaceA_8); $interfaceA_array=array_count_values($interfaceA);

uniqid() in javascript/jquery?

别说谁变了你拦得住时间么 提交于 2019-12-18 16:12:14
问题 what's the equivalent of this function in javascript: http://php.net/manual/en/function.uniqid.php Basically I need to generate a random ID that looks like: a4245f54345 and starts with a alphabetic character (so I can use it as a CSS id) 回答1: Try this (Work in php). $prefix = chr(rand(97,121)); $uniqid = $prefix.uniqid(); // $uniqid = uniqid($prefix); Try this for JavaScript:: var n = Math.floor(Math.random() * 11); var k = Math.floor(Math.random() * 1000000); var m = String.fromCharCode(n) +

strptime in Javascript

拥有回忆 提交于 2019-12-11 03:37:32
问题 I have a date input field that allows the user to enter in a date and I need to validate this input (I already have server side validation), but the trick is that the format is locale dependent. I already have a system for translating the strptime format string to the the user's preference and I would like to use this same format for validating on the Javascript side. Any ideas or links to a strptime() implementation in Javascript? 回答1: After a few days of googling I found this implementation

preg_match_all JS equivalent?

不问归期 提交于 2019-12-03 11:24:09
问题 Is there an equivalent of PHP's preg_match_all in Javascript? If not, what would be the best way to get all matches of a regular expression into an array? I'm willing to use any JS library to make it easier. 回答1: You can use match with the global modifier: >>> '1 2 3 4'.match(/\d/g); ["1", "2", "3", "4"] 回答2: John Resig has written about a great technique on his blog called 'Search and dont replace' It works using javascript's replace function, which takes a callback function, and returns

str_shuffle() equivalent in javascript?

☆樱花仙子☆ 提交于 2019-12-01 22:14:11
问题 Like the str_shuffle() function in PHP, is there a function similar in shuffling the string in javascript ? Please help ! 回答1: No such function exist, you'll write one yourself. Here's an example: function shuffle(string) { var parts = string.split(''); for (var i = parts.length; i > 0;) { var random = parseInt(Math.random() * i); var temp = parts[--i]; parts[i] = parts[random]; parts[random] = temp; } return parts.join(''); } alert(shuffle('abcdef')); 回答2: You could use php.js implementation

How can i use preg_match in jQuery?

老子叫甜甜 提交于 2019-11-30 17:44:26
问题 Can i use preg_match to validate phone number in jQuery? Here is my code which does not work if (!preg_match("/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/", phone.val() )) { phone.addClass("needsfilled"); phone.val(phonerror); } HTML <input id="phone" type="text" value="" name="phone" /> Could any one help plz 回答1: Javascript includes a regular expression engine, which is accessed via the string.match() , string.replace() and string.split() functions. For example: var mystring = "this is a sentence";

uniqid() in javascript/jquery?

大兔子大兔子 提交于 2019-11-30 13:57:13
what's the equivalent of this function in javascript: http://php.net/manual/en/function.uniqid.php Basically I need to generate a random ID that looks like: a4245f54345 and starts with a alphabetic character (so I can use it as a CSS id) Manish Trivedi Try this (Work in php). $prefix = chr(rand(97,121)); $uniqid = $prefix.uniqid(); // $uniqid = uniqid($prefix); Try this for JavaScript:: var n = Math.floor(Math.random() * 11); var k = Math.floor(Math.random() * 1000000); var m = String.fromCharCode(n) + k; Try PHPJS's website, specifically the page for uniqid I have been using this ... I use it

nl2br() equivalent in javascript [duplicate]

陌路散爱 提交于 2019-11-29 13:06:52
Possible Duplicate: jQuery convert line breaks to br (nl2br equivalent) Currently I add <BR> for each evt.which == 13 . Is there a nl2br() for JavaScript, so I can do away with this evt.which == 13 ? How different is this from php.js $('#TextArea').keypress(function(evt) { if (evt.which == 13) { var range = $('#TextArea').getSelection(); var image_selection = range.text; $('#TextArea').replaceSelection('<BR>'); $('#TextArea1').html($('#TextArea').val()); } }); oezi Take a look at nl2br on php.js which seems exactly what you're looking for. Basically, it's: function nl2br (str, is_xhtml) { if

array_count_values for JavaScript instead

落爺英雄遲暮 提交于 2019-11-28 12:50:06
I have the following PHP-script, now I need to do the same thing in JavaScript. Is there a function in JavaScript that works similar to the PHP function, I have been searching for days but cannot find anything similar? What I want to do is to count the number of times a certain word is being used in an array. $interfaceA = array($interfaceA_1,$interfaceA_2,$interfaceA_3,$interfaceA_4,$interfaceA_5,$interfaceA_6,$interfaceA_7,$interfaceA_8); $interfaceA_array=array_count_values($interfaceA); $knappsatsA = $interfaceA_array[gui_knappsats]; $touchpanelA = $interfaceA_array[gui_touchpanel];