MOLEDesign
MOLEDesign

Reputation: 488

Preserve a '+' symbol in a URL variable

I am working with a SOAP feed at the moment that requires the passing of some special variables (pulled from another feed)

eg.

+blPsNeYyjvRQhCiHQEISg==

It is absolutely vital that the variable be posted exactly as is, but when I am rendering it within a PHP script.. it is treating the + symbol as a + action and stripping it and trying to perform the calculation

So if I had blPsNeYyj+vRQhCiHQEISg== the string is posted as blPsNeYyj + vRQhCiHQEISg and causes errors.

If I replace the + with %2b it works, so I tried URL encode, and it still fails. This is because the == at the end has to stay as ==

So in summary I need all instances of + replaced by %2b

I have tried

$service = preg_replace("/\+/","%2B",$service);

but this also fails and just returns NULL

ANSWER :

Kept it simple to illustrate the process but to get the feed to work properly I had to use a very bizarre process

$service = (urlencode($_REQUEST['service']));

$service = substr($service,0,-6);

$service = $service.'==';

Encode it to preserve the + but this then converted the '=' to %3d which stopped the SOAP feed working.

Remove the %3d%3d

Readd two '==' and submit

and it works...

Go figure..

Upvotes: 1

Views: 488

Answers (2)

Carlos M. Meyer
Carlos M. Meyer

Reputation: 446

You need urlencode() it:

$service = urlencode($service);

Build your url

$url = "http://.....?service=".$service;

and when receiving it:

$service = urldecode($_GET['service']);

Upvotes: 2

Jack hardcastle
Jack hardcastle

Reputation: 2875

Your regex was almost correct

This occurs because the + character is used as an expression/a quantifier in the above regular expression, so you have to escape it with a backslash\

This works on my server

<?php

$sub = '+blPsNeYyjvRQhCiHQEISg==';

echo preg_replace('/\\+/', '%2b', $sub);

outputting

%2bblPsNeYyjvRQhCiHQEISg==

Upvotes: 1

Related Questions