Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JavaScript to stripslashes ? possible

I'm using ajax to grab a URL. The problem is the URL has slashes in it and when the JQuery load takes place afterwords it will not load the page.

AJAX success bit:

success: function(data) {
 $('#OPTcontentpanel').load(data.OPTpermalink);

PHP

echo json_encode( array('OPTpermalink'=>$OPTpermalink,));

AND the response

http:\/\/www.divethegap.com\/update\/options\/padi-open-water\/

So need to strip the slashes. I know how to do it in PHP but not in AJAX JavaScript.

Any ideas?

Marvellous

like image 977
RIK Avatar asked Mar 16 '11 13:03

RIK


People also ask

Is there a JavaScript equivalent to PHP's stripslashes?

Here’s what our current JavaScript equivalent to PHP's stripslashes looks like. You you can install via npm install locutus and require it via require ('locutus/php/strings/stripslashes') . You could also require the strings module in full so that you could access strings.stripslashes instead.

What is the use of stripslashes?

stripslashes (string $string): string Un-quotes a quoted string. stripslashes () can be used if you aren't inserting this data into a place (such as a database) that requires escaping. For example, if you're simply outputting data straight from an HTML form.

Does stripslashes() make the string longer for the query?

After several hours I found that stripslashes () made the string longer and hence it wasn't "equal" for the query. This code shows the behavior (copy into "test.php"). Replacing stripslashes worked for me. Rather use str_replace than explode/implode for your purpose. A replacement that should be safe on utf-8 strings.

Is stripslashes() recursive?

stripslashes () is not recursive. If you want to apply this function to a multi-dimensional array, you need to use a recursive function. $value = is_array($value) ? Array ( [0] => f'oo [1] => b'ar [2] => Array ( [0] => fo'o [1] => b'ar ) )


1 Answers

A new answer to an old question:

String.prototype.stripSlashes = function(){
    return this.replace(/\\(.)/mg, "$1");
}

Example of use:

var str = "You\'re slashed \/\\..\/\\"; // Text from server
str = str.stripSlashes() ;

output:

You're slashed /\../\
like image 103
ColBeseder Avatar answered Oct 06 '22 03:10

ColBeseder