Page 1 of 1

help with regex statment

Posted: Tue Jun 27, 2006 5:09 pm
by fransie
hate to ask for help,
but i'm not strong in the are of regex,
but i have a string, where i have a few smaller strings,
that can look likt this:

Code: Select all

${*}{*}
and basicly, i need to get the first *, and the second * into a array.
so basicly, if i have a string like this:

Code: Select all

some text, some stuff ${wow}{looky_here} also ${look}{here}
i need to get those two strings out, one at a time, so i have two arrays, that look like this:

Code: Select all

array1
   (
   [0]wow
   [1]looky_here
   )

array2
   (
   [0]look
   [1]here
   )
so, can any one provide me with some php code to get these?
preferably using the preg_match statment like this?

Code: Select all

while(preg_match($pattern, $string, $regs)) {
   //do stuff like
   print_r($regs);
   }
so, any help, would(or can) be nice.

Posted: Tue Jun 27, 2006 6:03 pm
by feyd
"ummmm" is probably not the best title, but I'd guess you know that already. We like to see titles be a little bit more specific. Anyway, something to work on for later.

You're looking to use preg_match_all().

The pattern you may be looking for is

Code: Select all

#\$\{([a-z_]+)\}\{([a-z_]+)\}#i

Posted: Tue Jun 27, 2006 6:06 pm
by fransie
thx, i'll see if this works,
also, i don't want to match all of the codes at once,
because there is specific things i do to them, before i go on to the next one.
so that is why i use the while statement.

Posted: Wed Jun 28, 2006 2:36 am
by Verminox
fransie wrote:thx, i'll see if this works,
also, i don't want to match all of the codes at once,
because there is specific things i do to them, before i go on to the next one.
so that is why i use the while statement.
preg_match will match one at a time, and the while look will help you to continue through the rest of the string. Only preg_match_all() matches all the occurences of the pattern in a string at once.

Posted: Thu Jun 29, 2006 12:18 pm
by fransie
ok, so i have my regex statment.
which is

Code: Select all

#\$\{([a-z_]+)\}\{([a-z_]+)\}#i
but i changed it to

Code: Select all

#\$\{([\w]+)\}\{([\w]+)\}#i
so, basicly, that /w will make that any alpha-num chaters will be accpeted, but
i want it to include *,#,@,!,%,& charters too(just those)
any idea on how to do that(i read some tutorial files, but i'm never good at reading those)

Posted: Thu Jun 29, 2006 3:20 pm
by Robert Plank
Yeah instead of

Code: Select all

[\w]
you can say

Code: Select all

[\w\*\#@!%&]

Posted: Mon Jul 03, 2006 10:16 pm
by feyd
be aware that \w matches far more characters than just alphanumerics: viewtopic.php?p=245010#245010

Posted: Tue Jul 04, 2006 10:21 pm
by printf
Just another way...

Code: Select all

<?

function array_builder ( $parts )
{
	global $data;
	$data[] = array ( $parts[2], $parts[4] );
}

$data = array ();

$str = 'some text, some stuff ${wow}{looky_here} also ${look}{here}';

preg_replace_callback( "/(\\$\{)(.*)(\}\{)(.*)(\})/U", "array_builder", $str );

print_r ( $data );

?>