0

I am passing a PHP string into onclick. This text comes from YouTube (a video description). In some cases the string breaks the code.

Characters \n \r ' and " are removed so it is not obvious what character is the cause.

echo "<span onclick='update_hits(\"".$id."\");vidtxt=\"".$desc6."\";openVideo(\"".$v."\",\"".$sn." : ".addslashes($title)."\",0)'><img src='".$apng."' border='0' />Play large</span>";

I have tried everything I can think of to solve the issue but have not succeeded.

$desc6=nl2br($desc6);
$desc6=preg_replace('/[\x00-\x1f]/', '', $desc6);
$desc6=preg_replace('/\xc2[\x80-\x9f]/', '', $desc6);
$desc6=preg_replace('/[\x7f]/', '', $desc6);
$desc6=nl2br($desc6);
$desc6 = iconv("UTF-8", "UTF-8//IGNORE", $desc6); 
$desc6=preg_replace('~\p{C}+~u', '', $desc6);
$desc6=htmlspecialchars($desc6);
$desc6=preg_replace('/[\x00-\x1F\x7F]/', '', $desc6); 
$desc6=ereg_replace('[[:cntrl:]]', '', $desc6); 
$desc6 = str_replace(chr(127), "", $desc6); 
$desc6=preg_replace('/\bKa(\W|$)/i', '', $desc6);
$desc6=addslashes($desc6); 

Most description strings work fine, even those with text in other languages. But something breaks this sometimes.

Stripping to just ascii, which would mean that I cannot display non-english languages, does work.

$desc6=preg_replace('/[^(\x20-\x7F)]*/','', $desc6);

The following allows all descriptions to work, but languages like Korean don't appear properly:

$desc6 = iconv("ISO-8859-1", "UTF-8//IGNORE", $desc6); 

Any ideas?

ps:
$desc6=json_encode($desc6); breaks all :(

5
  • Do you even know what this is code does? I mean, you are doing some steps multiple times (e. g. nl2br, removing 0x7F). Commented Jun 7, 2012 at 18:09
  • They are filtering the UTF. I'll remove redundancy when I find the solution. Commented Jun 7, 2012 at 18:14
  • does your HTML page make sure its charset is UTF-8? <meta http-equiv="Content-Type" content="text/html; charset=utf-8">? Also do you have an example string that breaks your code? Link to your application? Commented Jun 7, 2012 at 18:21
  • Example is the description to this video : youtube.com/watch?v=r5RzCBBNgv4 On YouTube the description doesn't have some wierd symbols that I see (I get this text from gdata). Commented Jun 7, 2012 at 18:31
  • Yes, it is utf-8 in the header. Good idea though. Commented Jun 7, 2012 at 18:35

2 Answers 2

2

All you need is two functions:

Both in action:

echo "<span onclick='".htmlspecialchars("update_hits(".json_encode($id).");vidtxt=".json_encode($desc6).";openVideo(".json_encode($v).",".json_encode($sn." : ".$title).",0)", ENT_QUOTES)."'><img src='".htmlspecialchars($apng, ENT_QUOTES)."' border='0' />Play large</span>";
Sign up to request clarification or add additional context in comments.

8 Comments

Have tried this but it didn't help. Thanks for your reply though!
This doesn't look right to me. There seems to ' and "s missing ? I tried vidtxt=json_encode(\"".$fgh2."\"); but without success - all broken. Need ti add json script?
@SteveClarke Please try the example as I wrote it. It doesn’t need to be changed or adjusted in any way. It’s ready to be used as it is.
Also tried vidtxt=\"".json_encode($desc6)."\"; Doesn't work - breaks all.
The error is in $desc6 so I tried: $desc6=json_encode($desc6); first also. Same - so it is not your code but the json_encode() not working with the utf-8 strings
|
0

.addslashes($title)."\",0) Shouldn't that be "\""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.