A couple of functions that may be useful to you

Translation of the original in Catalan. Read it in: CatalàCastellano

For a few days now I think I'm ceasing to be human; Daphne has just called me and she sounded like an owl. I wasn't able to string a proper sentence together. What does this have to do with the code below? well, that I'm programming a kind of webspider or robot or call it whatever you like for a Downcenter thing, in order to feed information into the DB of the next piece of software that comes out on 1 December.

Since I can't explain more details about what I'm programming I'll limit myself to pasting a couple of functions that may be useful to more than one person programming in PHP. You surely know that when you fill in an HTML form and it sends a request, the URL that is sent, as you can check in the browser's address bar, takes on a weird shape. That is, spaces become "+" symbols and the extended ASCII characters, above code 127, are encoded in hexadecimal codes.

Well, these functions do exactly that: you pass them a string, for example a URL in plain 8-bit form, and they convert it into the format I mentioned before. The other function does the opposite, it goes from the encoded format to the normal one. I hope it's as useful to you as it has been to me.


function cadenaNormal2Hex ($cadena) {
	// Canviem simbols Hex del mig de la cadena
	$llarg = strlen($cadena);
	for ($i=0;$i<$llarg;$i++) {
		$numascii=ord($cadena[$i]);
		if ( $numascii>127 ) {  // És text pla (???)
			$car=strtoupper(dechex(ord($cadena[$i])));
			$cadena = substr_replace ($cadena,"%".$car,$i,1);
		}//if
	}//for
	// Canvia el + per l'espai
	while ($pos = strpos($cadena," ")) {
		$cadena = substr_replace ( $cadena, "+", $pos,1);
	} //while
	return $cadena;
} // function

function cadenaHex2Normal ($cadena) {
	// Canvia el + per l'espai
	while ($pos = strpos($cadena,"+")) {
		$cadena = substr_replace ( $cadena, " ", $pos,1);
	} //while
	// Canviem simbols Hex del mig de la cadena
	while ( $pos = strpos($cadena,"%") ) {
		$codi=substr($cadena,$pos+1,2);
		$car=chr(hexdec($codi));
		$cadena = substr_replace ($cadena,$car,$pos,3);
	}//while
	return $cadena;
} // function