1

I am having a problem with the special characters in my script:

This is what I have so far:

$curlstrip = explode("&", $data);
$filename = substr(htmlEntities($curlstrip[5]), 2);

and if $data contains any special charaters like ' which is ', then instead of getting the chunk of string that I need, I get only the first part. A more detailed example:

$data = "12er&sdsretdgsd&file=Chris ' 19 &blabla"

the script will read the ' after Chris as ' and $curlstrip[5] will have a different value.

Hope is clear enough.

LE. Following this example:

$data = "12er&sdsretdgsd&file=Chris ' 19 &blabla"
$curlstrip = explode("&", $data);
$curlstrip[0] = '12er';
$curlstrip[1] = 'sdsretdgsd';

but

$curlstrip[2] = 'file=Chris' instead of 'file=Chris ' 19'

and that is because the ' is being read as

'

Thx,

Cristian.

Chris19
  • 210
  • 7
  • 17
  • Can you add a bit more detail about what you are doing - where the data comes from - and what you *intend* to do with it (where `$data` goes to)? – Pekka Nov 01 '10 at 16:20
  • 1
    I can't change the format of $data, so I don't think it matter where is coming from. Thx. – Chris19 Nov 01 '10 at 16:21
  • $curlstrip[5] gets entered in a database, but my problem is that I can't get around these special characters. – Chris19 Nov 01 '10 at 16:22
  • @Chris you're still not giving enough information to give you a hint on what to do with the string. Maybe show the whole process and the point at which it crashes. – Pekka Nov 01 '10 at 16:38
  • @Pekka the script almost works, except the part where, if the string contains single quotes they are being read as '$#39;' – Chris19 Nov 01 '10 at 16:46

2 Answers2

1

Try:

$curlstrip = explode("&", html_entity_decode($data));
$filename = substr(htmlEntities($curlstrip[5]), 2);

As I cant replicate your error given the code supplied, I have a hunch- try:

$data=str_replace("'","'", $data);
$curlstrip = explode("&", html_entity_decode($data));
$filename = substr(htmlEntities($curlstrip[5]), 2);

If, for whatever reason, you have && (two ampersands) occurring in $data next to one another, it will interfere with the explode function.

SW4
  • 69,876
  • 20
  • 132
  • 137
  • Hmmm, using the code you've given- I cant replicate the problem with: $data = "12er&sdsretdgsd&file=Chris ' 19 &blabla"; $curlstrip = explode("&", $data); print_r($curlstrip); – SW4 Nov 01 '10 at 17:06
  • I think we are getting somewhere Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '95, The (1995-09-29)(SNK)(en-jp)[!][NGCD-084, NGCD-084E].zip', file_id = 'id0=0y' at line 1 – Chris19 Nov 01 '10 at 17:30
  • @Ergo, using the script provided and adding '".addslashes($filename)."' before I enter the data in the database solved my problem. Thanks. – Chris19 Nov 02 '10 at 05:12
0

Make htmlentities the last thing you call on the data.

$filename = htmlentities(substr($curlstrip[5], 2));

Aside from that there are functions to deal with what you're doing. parse_str will create an array from a query string. parse_url can be used to extract the query string from a url.

Galen
  • 29,976
  • 9
  • 71
  • 89
  • what do you mean by "Make htmlentities the last thing you call on the data"? Thx. – Chris19 Nov 01 '10 at 16:29
  • Same thing, I get only the first part of the $curlstrip[5], before the ' . I updated my question with more info. Thx. – Chris19 Nov 01 '10 at 16:37
  • 1
    Dont you want to decode, not encode the characters? This would produce more ampersands, not reverse back into say, apostrophes – SW4 Nov 01 '10 at 17:10