help with regex statment

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
fransie
Forum Newbie
Posts: 3
Joined: Tue Jun 27, 2006 4:23 pm

help with regex statment

Post 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.
Last edited by fransie on Tue Jun 27, 2006 6:09 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
fransie
Forum Newbie
Posts: 3
Joined: Tue Jun 27, 2006 4:23 pm

Post 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.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post 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.
fransie
Forum Newbie
Posts: 3
Joined: Tue Jun 27, 2006 4:23 pm

Post 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)
Robert Plank
Forum Contributor
Posts: 110
Joined: Sun Dec 26, 2004 9:04 pm
Contact:

Post by Robert Plank »

Yeah instead of

Code: Select all

[\w]
you can say

Code: Select all

[\w\*\#@!%&]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

be aware that \w matches far more characters than just alphanumerics: viewtopic.php?p=245010#245010
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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 );

?>
Post Reply