Jump to content

Archived

This topic is now archived and is closed to further replies.

neuroxik

preg_replace for hex

Recommended Posts

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:

[code=php:0]$artist = "<a href=\"?a=$encartist\">$aname</a>";[/code]

$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!

Share this post


Link to post
Share on other sites
I'm confused. Can't you urlencode both $encartist and $aname? [url=http://xkr.us/articles/javascript/encode-compare/]Javascript also has this ability[/url].

Share this post


Link to post
Share on other sites
[quote author=effigy link=topic=101118.msg399908#msg399908 date=1153328943]
I'm confused. Can't you urlencode both $encartist and $aname? [url=http://xkr.us/articles/javascript/encode-compare/]Javascript also has this ability[/url].
[/quote]

Yeh, there's no problem with the url encoding, which gives as a result a url which looks like [b]britney+spears[/b] instead of [b]britney spears[/b] and other char replacements, but when I'm using AJAX, it won't work, so I had to addslashes and str_replace [b]&[/b] -> [b]%26[/b], 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).

Share this post


Link to post
Share on other sites
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?

[code]
<?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));
}
?>[/code]

Share this post


Link to post
Share on other sites
[quote author=neuroxik link=topic=101118.msg400320#msg400320 date=1153389711]
None of them work, but thanks!
[/quote]

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

[quote]
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
[/quote]

Clicking the Javascript part results in:

[quote]
Gro%DFe%20%26amp%3B%20sch%F6ne.
[/quote]

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?

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.