piece of a variable

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

piece of a variable

Postby Xerpher » Fri Oct 04, 2002 1:20 pm

How would I see if $var has "access" in the beginning of it?

Jay

Postby Jay » Fri Oct 04, 2002 1:58 pm

What are you looking for?

1. A variable who's name starts with 'access'?

Code: Select all

while(list($var,$val) = each($GLOBALS)) {
if (preg_match('^access'),$var) {
$theValue = $var;
break;
}
}


2. A variable which has access as the beginning of it's string value?

Code: Select all

while(list($var,$val) = each($GLOBALS)) {
if (preg_match('^access'),$val) {
$theValue = $var;
break;
}
}


3. Anything else? Can you see a pattern forming?

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Fri Oct 04, 2002 2:06 pm

Thanks, its #2 I need :D

Jay

Postby Jay » Sat Oct 05, 2002 9:58 am

OK, but the correct code should have been

Code: Select all

while(list($var,$val) = each($GLOBALS)) {
if (preg_match('^access'),$val) {
$theValue = $val;
break;
}
}

I put $theValue = $var instead of $val, but you would have figured that part out, wouldn't you ;)

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Sat Oct 05, 2002 10:23 am

Lol, well that explains why it didn't work :) you fixed it before I even said it didn't work :D

Jay

Postby Jay » Sat Oct 05, 2002 11:02 am

Well, let me know if it works, because I made up that REGEX up on the spot, and it's definately not my strong point. I just happen to know that the ^ character means at the beginning!

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Postby Xerpher » Sun Oct 06, 2002 5:14 pm

It worked.... I just ad to fix two tny things :D

Code: Select all

while(list($var,$val) = each($_GET)) {
   if (preg_match('^access^',$val)) {
      $theValue = $val;
      break;
   }
}

See, I had to move the ')' to after the $val, and had to put another '^' at the nd of access. But still thanks, I've never used preg_match before so would've had no idea about those symbols and which ones to use.

Jay

Postby Jay » Sun Oct 06, 2002 11:00 pm

Ah yes true, how could I have been so daft to leave out the $val out of the function! Not sure why you'd need to add a second ^ though, because that means at the beginning, so it's only good once. But if it works, all is well :)


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 3 guests

cron