Shoutbox

[PHP] Parsing the media string - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: [PHP] Parsing the media string (/showthread.php?tid=65276)

[PHP] Parsing the media string by Matti on 08-23-2006 at 10:05 AM

Ok, this simply sucks. :P

I want to parse a string like:

code:
$psm = "\\0Music\\01\\0Nu op Q: {0} - {1}\\0When It All Falls Apart\\0Veronicas\\0\\0\\0";
to an array containing its elements, like:
code:
$match = array(4) {
  [0]=> string(5)
  "Music"
  [1]=> string(18)
  "Nu op Q: {0} - {1}"
  [2]=> string(23)
  "When It All Falls Apart"
  [3]=> string(9)
  "Veronicas"
}
using preg_match(). But now I have problems with the backslash... uh?!
This code fails:
code:
preg_match("/^\\0([a-zA-Z]+)\\01\\0(.*?)\\0(.*?)\\0(.*?)\\0(.*?)\\0(.*?)\\0(.*?)$/", $psm, $ico_match);
And while I tried to look for the fault, I found that this:
code:
preg_match("/0([a-zA-Z]+)/", $psm, $ico_match);
finds "Music", while this one:
code:
preg_match("/\\0([a-zA-Z]+)/", $psm, $ico_match);
fails. :-/

Any ideas? :P
RE: [PHP] Parsing the media string by RaceProUK on 08-23-2006 at 10:59 AM

preg_match("/^\\\\0([a-zA-Z]+)\\\\01\\\\0(.*?)\\\\0(.*?)\\\\0(.*?)\\\\0(.*?)\\\\0(.*?)\\\\0(.*?)$/", $psm, $ico_match);

Note the quadruple backslashes.

Explanation: In JScript, the backslash is used to escpace certain special characters that are otherwise unprintable. To print a backslash therefore, you need to escape that too i.e. \\. So, for a double backslash, both need escaping, so \\\\.


RE: [PHP] Parsing the media string by Matti on 08-24-2006 at 07:45 AM

OMFG That's weird, but it worked! :P
* Matti notes: catching a backslash in PHP -> 4 backslashes

Thanks RP! ;)