About array_search and preg_match,why 1,2, is not output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saloufeng
Forum Newbie
Posts: 8
Joined: Fri Aug 01, 2003 7:49 am

About array_search and preg_match,why 1,2, is not output

Post by saloufeng »

Code: Select all

<?php
	$a = '11';
	$b = '11,';
	$c = explode(',', $b);
	echo $c;
	if(array_search($a,$c))&#123;
	  echo 1;
	&#125;
	if(preg_match("#$".$a.",#",$b))&#123;
	  echo 2;
	&#125; 
	if(preg_match("#".$a.",#",$b))&#123;
	  echo 3;
	&#125; 
?>
why 1,2, is not output?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

I'm not sure why array_search is acting up. I just tried in_array($a,$c) and that worked fine.

Your number two is broken because you are using an unescaped dollarsign in the doubly quote string. Either escape the dollar sign or change to single quotes.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

On number 2, haven't you flipped your '$' and '^'? Isn't '^' beginning of string and '$' end of string? So of course it doesn't match as it is impossible to match something that specifies the end of string before the beginning of string.
saloufeng
Forum Newbie
Posts: 8
Joined: Fri Aug 01, 2003 7:49 am

Post by saloufeng »

nielsene.
yea,I'm sorry,I made mistakes.
Thank you!
Post Reply