note 70356 added to function.sscanf

From: Date: Thu, 12 Oct 2006 20:02:08 +0000
Subject: note 70356 added to function.sscanf
Groups: php.notes 
Request: Send a blank email to php-notes+get-118471@lists.php.net to get a copy of this message
Important warning about the last note by "anonymouse" (quoting): ---------------------------------------------------------------------- sscanf($string_to_parse,'%d %[^$]s',$num,$text); This conversion command says "look for an integer, then a space, then any string up to the end of the string" ---------------------------------------------------------------------- This won't do what you think. You have the ^ and $ characters inside square brackets: $text will be empty if $string_to_parse contains for example "123 $". What that pattern really means is: match an signed integer, followed by a whitespace, followed by a string *NOT* containing a dollar sign, followed by an 's'. To do what you want i'm afraid you really must use ereg. Scanf is *not* a regular expression parser. It was designed to be used in plain C, where you don't have arbitrary length strings natively. Also, in scanf patterns you don't have an equivalent to matching the beginning/end of string, like you have with ^ and $ in regular expressions. In PHP, you should use something like: if (ereg('^([+-]?[0-9]+) (.+)$', $string_to_parse, $regs)) { $num = $regs[1]; $text = $regs[2]; } This regular expression assumes you want a signed integer at the beginning, and you don't mind if it starts with 0 when it is non-zero, e.g. 0123, +0321 or -0456 is also valid. ---- Server IP: 66.163.161.117 Probable Submitter: 89.26.191.34 ---- Manual Page -- http://www.php.net/manual/en/function.sscanf.php Edit -- https://master.php.net/note/edit/70356 Del: integrated -- https://master.php.net/note/delete/70356/integrated Del: useless -- https://master.php.net/note/delete/70356/useless Del: bad code -- https://master.php.net/note/delete/70356/bad+code Del: spam -- https://master.php.net/note/delete/70356/spam Del: non-english -- https://master.php.net/note/delete/70356/non-english Del: in docs -- https://master.php.net/note/delete/70356/in+docs Del: other reasons-- https://master.php.net/note/delete/70356 Reject -- https://master.php.net/note/reject/70356 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#118471) next »