Translate javascript function in php - javascript

I have the following code in jS :
function stripHTMLWithLinks(c) {
var BodyContents = /([\s\S]*\<body[^\>]*\>)([\s\S]*)(\<\/body\>[\s\S]*)/i ;
var h = c.match(BodyContents);
if (h != null && h[2]) {
c = h[2];
}
c = c.replace(/<a\s.*?href\s*=\s*"(.*?)".*?>(.*?)<\/a>/gi, "$2 [$1]");
c = c.replace(/<a\s.*?href\s*=\s*'(.*?)'.*?>(.*?)<\/a>/gi, "$2 [$1]");
c = c.replace(/\/\/--\>/gi, "");
c = c.replace(/(\n)/gi,"");
c = c.replace(/(\r)/gi,"");
c = c.replace(/<title[^\>]*\>[\s\S]*\<\/title\>/gi,"");
c = c.replace(/<br\s*\/\s*>/gi,"\n");
c = c.replace(/(<\/h.>|<\/p>|<\/div>)/gi, "$1\n\n");
c = c.replace(/<[^>]+>/g,"");
c = c.replace(/</g,"<");
c = c.replace(/>/g,">");
c = c.replace(/ /g," ");
return c;
}
I need a function that will extract the body content. All the examples that I saw here were kinda fixed on a particular example , and i need it on dynamic basis. This function can do it for me, I just need it in php.

This should be really easy to transcode to PHP.
Look for preg_match and preg_replace in the PHP Manual.

Try strip_tags.
$html = "<body><a href='http://example.com'>Something</a> <b>big</b> is coming</body>";
echo strip_tags($html); // Something big is coming

Related

After Addition two variables in javascript returning some garbage values

Hi I have two variables in JS Like:
var a = 223620.42
var b = 1200.1234
I am using Calculation Like:
var c = parseFloat(a) + parseFloat(b);
So the result should be = 224820.5434
But it returning 224820.54340000002
Please Suggest me what I am doing wrong here. Thanks in advance
Just Round-off the last Values. See the Code Below:
var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
var numb = c.toFixed(4);
alert(numb);
hope it will solve you problem.
Try Using "toPrecision" method, and specify the desired length of the number.
var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
c= c.toPrecision(3)

How to create variable with concatenation in javascript?

I have very long code and want to simplify it. I don't think so we can use string concatination to create different variable name. Is there any other way.
If(Something){
var a = some.Test1.x;
var b = some.Test1.y;
var c = some.Test1.z;
var d = some.Test1.p;
}
If(SomethingElse){
var a = some.Test2.x;
var b = some.Test2.y;
var c = some.Test2.z;
var d = some.Test2.p;
}
If(OneMore){
var a = some.Test3.x;
var b = some.Test3.y;
var c = some.Test3.z;
var d = some.Test3.p;
}
May be create some function like this, i know this is not right but anything similar to that.
function test(s){
a = some.s.x;
b = some.s.y;
c = some.s.z;
d = some.s.p;
}
You can use array-like syntax
function test(s) {
var a,b,c,d;
if (s === Something) {
test = "Test1";
}
if (s === SomethingElse) {
test = "Test2";
}
if (s === OneMore) {
test = "Test3";
}
a = some[test].x;
b = some[test].y;
c = some[test].z;
d = some[test].p;
}
This is a much improved version of the functionally equivalent, but never-to-be-used, eval syntax:
eval("a = some."+test+".x");
function test(index){
t = some["Test" + index];
a = t.x;
b = t.y;
c = t.z;
d = t.p;
}
and the execution:
test(1); test(2); test(3)

JavaScript Addition Operators not work

I try using javascript but the addition is not correct, live code here
function hitung2() {
var a = $(".a2").val();
var b = $(".b2").val();
c = a * b; //a kali b
d = c + a;
$(".c2").val(c);
$(".d2").val(d);
}
function hitung2() {
var a = parseInt($(".a2").val());
var b = parseInt($(".b2").val());
c = a * b; //a kali b
d = c + a;
$(".c2").val(c);
$(".d2").val(d);
}
Or if you going to work with fractions then use parseFloat() function in same way as it described above.
You need to change the string that you get from .val to ints with parseInt()
var a = parseInt($(".a2").val());
var b = parseInt($(".b2").val());
Updated fiddle. http://jsfiddle.net/b39Lz/32/
use parseInt()
var a = $(".a2").val();
var b = $(".b2").val();
temp1=parseInt(a);
temp2=parseInt(b);
c = temp1 * temp2;
d = c + temp1;
Your variables are String, so it uses String + operator, does concatenating the value. Convert string to int using: parseInt("10")
function hitung2() {
var a = $(".a2").val();
var b = $(".b2").val();
c = a * b; //a kali b
d = parseInt(c) + parseInt(a);
$(".c2").val(c);
$(".d2").val(d);
}
Avoid using parseInt() if you dont want to stick to integer calculations only.Try casting to general Number.
function hitung2() {
var a =Number( $(".a2").val());
var b = Number($(".b2").val());
var c = a * b; //a kali b
d =Number(c)+Number(a);
$(".c2").val(c);
$(".d2").val(d);
}

Need to strip a leading character (# or +) from this string

In the cases below I need to strip either the first "#" or "+" character for vars b & c so they can become vars a & a1 which are used to be a string in a new url that's built elsewhere.
Here is the code:
var b = "http://www.somewhere.com/search/#foo+bar+baz"
var c = "http://www.somewhere.com/search/++bar+baz"
var a = b.split("/")[4].split("+").slice(0, 1);
var a1 = c.split("/")[4].split("+").slice(0, 1);
Here is the fiddle.
This works for me on your fiddle.
var b = "http://www.somewhere.com/search/#foo+bar+baz"
var c = "http://www.somewhere.com/search/++bar+baz"
var a = b.split("/")[4].split("+").slice(0, 1);
if(a[0].indexOf('#') != -1){a = a[0].split('#')[1]}
var a1 = c.split("/")[4].split("+");
var i=0;
while(true){
if(a1[i] != ''){a1 = a1[i]; break;}
i++;
}
$("#result").html(a);
$("#result2").html(a1);
console.log(a);
console.log(a1);
Updated your fiddle
You can replace the first occurrence of a char with using a regex quite easily:
var a = b.replace(/[#+]/,'');
var a1 = c.replace(/[#+]/,'');
Without the g modifier it will only replace the first one it finds.

making relative url absolute using arbitrary root in javascript

Assuming that I'm on a page on a different domain (mydomain.com) and that the relative url only exists in code (not in the DOM)
How do I combine two arbitrary urls entirely in javascript?
var a = 'http://example.com/some/path/';
var b = '../other/path/';
var c = magicUrlCombine(a,b);
assert(c == 'http://example.com/some/other/path/');
It should also work for
var a = 'http://example.com/some/path/';
var b = 'http://pink-unicorns.com/some/other/path/';
var c = magicUrlCombine(a,b);
assert(c == 'http://pink-unicorns.com/some/other/path/');
EDIT:
I'm looking for a completely general function for combining an absolute url with an arbitrary url. The same logic as the browser uses for resolving links but for urls that are not in the HTML of the page and/or not relative to the current location.href.
var a = 'http://example.com/a/b/c/';
var b = '../d/e/';
assert(c == 'http://example.com/a/b/d/e/')
OR
var b = '/f/g/';
assert(c == 'http://example.com/f/g/')
OR
var b = 'http://jquery.com/h/i/';
assert(c == 'http://jquery.com/h/i/')
EDIT 2:
node.js has a url module that has the right functionality, but I haven't found a nice way of reusing it on the client side. (how to use node.js module system on the clientside)
I managed to hack my way through making it work but it's not really a solution I feel comfortable putting into a production site. Hackety hack
JQuery Mobile has it
$.mobile.path.makeUrlAbsolute(relPath, absPath)
console.log($.mobile.path.makeUrlAbsolute('../d/e/', 'http://example.com/a/b/c/'));
console.log($.mobile.path.makeUrlAbsolute('/f/g/', 'http://example.com/a/b/c/'));
console.log($.mobile.path.makeUrlAbsolute('http://jquery.com/h/i/', 'http://example.com/a/b/c/'));
all give the expected results
I have used on the server side, using NodeJS,
var url = require('url');
url.resolve(from, to);
in your case:
var a = 'http://example.com/some/path/';
var b = '../other/path/';
var c = url.resolve(a, b);
assert(c == 'http://example.com/some/other/path/');
var a = 'http://example.com/some/path/';
var b = 'http://pink-unicorns.com/some/other/path/';
var c = url.resolve(a, b);
assert(c == 'http://pink-unicorns.com/some/other/path/');
Couldn't resist having a go at a solution
var magicUrlCombine = function(a,b){
return (a + b).replace(/[\w\-\.]+\/..\/|\:\/\/[\w\-\.\/]+http/g,'');
}
works for both test cases and combinations of the two
http://jsfiddle.net/8HLeQ/2/
I assumed I understood the question but my fiddle returns two false. The examples are not obvious
http://jsfiddle.net/mplungjan/z5SUn/
function magicUrlCombine(a,b) {
var linkA = document.createElement('a');
linkA.href = a;
var linkB = document.createElement('a');
linkB.href = b;
return linkB.href.replace(linkB.host,linkA.host)
}
This is a possible, but untested, solution:
function magicCombine(a,b){
if(b.indexOf('://') != -1) return b;
var backs = 0;
var lastIndex = b.indexOf('../');
while(lastIndex != -1){
backs++;
lastIndex = b.indexOf('../', lastIndex+3);
}
var URL = a.split('/');
//Remove last part of URL array, which is always either the file name or [BLANK]
URL.splice(URL.length-1, 1)
if(b.substr(0,1) == '/')
b = b.substr(1);
var toAdd = b.split('/');
for(var i = 0, c = toAdd.length-backs; i < c; ++i){
if(i < backs)
URL[URL.length - (backs-i)] = toAdd[backs+i];
else
URL.push(toAdd[backs+i]);
}
return URL.join('/');
}
Should take care of both cases...

Categories