Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular exp.

Status
Not open for further replies.

yp56

Programmer
Aug 30, 2001
83
FR
hello,

I've got a variable wich contains something like this :

$myvar = "textextext\FIELD1\textextexte\FIELD2\textextext\FIELD3\textext....."

And I want to get all field in an array. so I try to use ereg() like this :

ereg("^([^\\\\]*(\\\\[A-Z0-9]*\\\\)[^\\\\]*)$"), myvar, myarray);

And it doesn't work :c(
Can someone help me, please ? :c)
thaks a lot....
 
How 'bout

$myarray = preg_split ('/\\\\FIELD\d+\\\\/', $myvar);

______________________________________________________________________
TANSTAAFL!
 
Well, I'm the kind of person that only uses regex when there is no other solution. Call me a coward ;-).

If you do a simple
Code:
$my_array = explode('/',$myvar)
, you will receive an array, consisting of ("textextext", "FIELD1", "textextext", "FIELD2") etc... So, if this is the pattern, then that means even-numbered indexes have your data, while the odd-numbered ones have your field names. (
Thus $my_array[0] is "textextext"

and $my_array[1] is "FIELD1"

So, if you get the length of that array with $arr_len = sizeof($my_array), then you can loop through the array, assigning field names and data:
Code:
for($i = 0; $i < $arr_len; $i+=2) //iterate by 2
{
  echo $my_array[($i + 1)] . &quot; = $my_array[$i]<br>\n&quot;;
}
This should output something like:

FIELD1 = textextext
FIELD2 = textextext
etc... -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Trust is Suspicion&quot;
(
 
ok thanks a lot both :c) i'll try these examples....
@++
 
yp56,

rycamor's method also has the advantage over mine that you can preserve those fieldnames if you ever needed to. ______________________________________________________________________
TANSTAAFL!
 
yes, I use :
foreach(explode(&quot;\\&quot;, $var) as $k=>$v){
if ($k % 2 == 1) {
...$v...
}
}

sleipnir214 ::
your mehod was very well but it gives me ('textextextex1', 'textextextex2', 'textextextex3', ...)
and I wanted ('FIELD1', 'FIELD2', ...) :cD
THX
 
My apologies, I must have misread your post. I thought the &quot;FIELDn&quot; tags were your field delimiters. ______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top