Something like this will work:

eregi("^id \{([a-z]*),([a-z]+),([a-z]+)\} \[(.+)\]$", $str, $regs);

$regs will be an array containing:

[0] => "id {name,title,nick} [http://www.php.net]";
[1] => "name"
[2] => "title"
[3] => "nick"
[4] => "http://www.php.net";

If course this isn't very foolproof or generic. For instance, this regex 
assumes that you have exactly three comma-delimited substrings between 
those curly braces and anything goes in the brackets. (It could be a URL, 
but it could also be anything else.)

This is assuming there are three items between those curly braces, however. 

To handle multiple configurations within the curly braces, you'd probably, 
as the boundaries would cause items in the array to be overwritten during 
matching. Something like this may work half-decent...

if (eregi("(id) \{(.*)\} \[(.*)\]$", $str, $regs))
{
    $p[0] = $regs[1];
    $p[1] = explode(",", $regs[2]);
    $p[2] = $regs[3];
}

$p should now look like the following:

[0] => "id"
[1][0] => "name"
[1][1] => "title"
[1][2] => "nick" )
[2] => "http://www.php.net";

That should do it. Of course, the regex again isn't foolproof, as you could 
have crazy stuff for a URL, and it doesn't bother to check if there's 
anything after a comma in the curly braces, but will handle any number of 
items between the braces, and it's enough to get you started.

J



Valentin V. Petruchek wrote:

> 
> ----- Original Message -----
> From: "Valentin V. Petruchek" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, December 03, 2001 6:35 PM
> Subject: [PHP] ereg help
> 
> 
>> I'm not new for php, but have no experience working with ereg functions.
> My
>> problem is the following:
>>
>> i have string.. for example
>>     $s="id {name,title,nick} [http://www.php.net]";;
>> i want to break it in several parts:
>> $p[0]="id";
>> $p[1][0] ="name";
>> $p[1][1] ="title";
>> $p[1][2] ="nick";
>> $p[2]="http://www.php.net";;
>>
>> The part in [] is not neccessary, count of {} elements is not less than 1
>> I can do it with string fucntions, but it seemes to me it's better to use
>> regular functions.
>>
>> Could anybody help me to solve this problem and advise resource for
> studying
>> regular expresions?
>>
>> Zliy Pes, http://zliypes.com.ua
>>
>>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to