Jump to content


Photo

preg_replace for hex


  • Please log in to reply
6 replies to this topic

#1 neuroxik

neuroxik
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 19 July 2006 - 04:17 PM

Hey everyone,

  I've been struggling on my own for a while with this issue and thought maybe someone here could help. Basically, I'm running into problems because I use an AJAX script to keep the page from reloading on each request, but my requests ( using GET ) must inlcude a variable and a value, for example:

$artist = "<a href=\"?a=$encartist\">$aname</a>";

$encartist only represents the urlencode'd artist name. The problem is some artists have special characters like ê,à, etc.. and I can't use a preg_replace that replaces these by let's say ê for ê because the ampersand in it awaits ( as I've understood it ) for a new variable, like in ?a=something&t=something. Whatever the reason is, it won't work. So I was wondering if you guys had something to let's say replace those special character (ê,à,è, etc) into hexdec's like %26 (amp) or whatever.

Thanks in advance!

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 19 July 2006 - 05:09 PM

I'm confused. Can't you urlencode both $encartist and $aname? Javascript also has this ability.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 neuroxik

neuroxik
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 19 July 2006 - 07:09 PM

I'm confused. Can't you urlencode both $encartist and $aname? Javascript also has this ability.


Yeh, there's no problem with the url encoding, which gives as a result a url which looks like britney+spears instead of britney spears and other char replacements, but when I'm using AJAX, it won't work, so I had to addslashes and str_replace & -> %26, but I don't want to str_replace every imaginable character, I know it may sound wierd, but with AJAX to send the requests, urlencode alone just won't work when you click on artists with special characters. I was wondering how I'd go about writing the preg_replace for all characters ( '/[^\x09\x0A\x0D\x20-\x7F]/e' ) to their hexadecimal value ( ex.: %26 ), cuz the only ones I found was the ord($0) to replace like the following which replaces something like { (which I don't want cuz it doesn't work with my ajax script).

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 19 July 2006 - 07:57 PM

I still don't follow entirely; perhaps due to my lack of experience with AJAX. Below is an example... Do you need a sprintf in Javascript?

<?php
	$string = 'Große & schöne.';
?>

<script type="text/javascript" language="javascript">
	function _escape(obj) {
		alert(escape(obj.innerHTML));
	}
</script>

Use Javascript's escape (click it =>) <span onclick="javascript:_escape(this)"><?php echo $string; ?></span>
<br /> or use PHP's urlencode:
<?php
	echo urlencode($string);
?>
<br /> or use PHP's printf:
<?php
	$pieces = str_split($string);
	foreach ($pieces as $piece) {
		printf('%%%X', ord($piece));
	}
?>

Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 neuroxik

neuroxik
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 20 July 2006 - 10:01 AM

None of them work, but thanks!

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 20 July 2006 - 02:00 PM

None of them work, but thanks!


You're not helping me help you.... What doesn't work? In my browser I see this:

Use Javascript's escape (click it =>) Große & schöne.
or use PHP's urlencode: Gro%DFe+%26+sch%F6ne.
or use PHP's printf: %47%72%6F%DF%65%20%26%20%73%63%68%F6%6E%65%2E


Clicking the Javascript part results in:

Gro%DFe%20%26amp%3B%20sch%F6ne.


Can you be clear on what you need? If this is AJAX, does that mean PHP cannot help at all? Is this purely javascript? What is the example code not doing that you need it to?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 neuroxik

neuroxik
  • Members
  • PipPipPip
  • Advanced Member
  • 43 posts

Posted 21 July 2006 - 05:14 AM

I'm very sorry on not having been clear enough. Fortunately, some javascript god has fixed my problem, the problem was not in the php, but in my ajax file that handled the requests. I'm greatly grateful for the time you spent trying to help me which wasn't obvious considering I had little information to give. Thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users