Mooticus Mooticus - 1 month ago 17
PHP Question

rawurlencode replace spaces with &

I have a form which is named 'q'. I'm using it for Confluence searching, using the REST API. To get multi-word search working, spaces need to be & symbols. The current code makes spaces +. How can i get spaces replacing themselves with & symbols? I've tried using str_replace, and i can't find much on the internet.

if (isset($_GET['q'])) {
$query = rawurlencode( strip_tags($_GET['q']));
$query = str_replace(' ', '&', $query);
$timestamp = time();
$baseUrl = 'https://mywebsite.atlassian.net/wiki';
$url = $baseUrl.'/rest/api/content/search?cql=space=KB%20AND%20type=page%20AND%20title~'.$query;

Answer

I'm not sure whether this is the "best" way to do it, but it also sounds like a pretty non-conformist request from Confluence (in terms of RFCs), but:

$query = str_replace('%20', '&', $query); // spaces are %20 after raw encoding
Comments