Current location: Hot Scripts Forums » Programming Languages » PHP » String to...


String to...

Reply
  #1 (permalink)  
Old 05-26-05, 01:59 PM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
String to...

i have string like this:
$mystr = "1,2,3";

and i want to use that string like the script (bold red text) below:
<?

$okvar = 1;
if ($okvar == (1||2||3)) {
echo "OK";
} else {
echo "ERROR";
}

?>

(1||2||3) is from $mystr
How to do that?
Reply With Quote
  #2 (permalink)  
Old 05-26-05, 02:03 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Well, not quite understand you, but:

$okvar = '(' . str_replace(',', '||', $mystr) . ')';
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #3 (permalink)  
Old 05-26-05, 02:14 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
I kinda understand but at the same time i don't
Ill just assume

PHP Code:

<?

$mystr 
"1,2,3";
$str=explode(","$mystr);

$okvar 1;
for(
$i=0$i count($str); $i++){
if(
$okvar == $str[$i]){
echo 
"OK";
} else {
echo 
"ERROR";
}
}

?>
Reply With Quote
  #4 (permalink)  
Old 05-26-05, 02:19 PM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
no, not like that.

+++++++++++++++++++
if $okvar must 1 or 2 or 3
+++++++++++++++++++

1 or 2 or 3 are from $mystr
Reply With Quote
  #5 (permalink)  
Old 05-26-05, 02:22 PM
shroder shroder is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
PHP has a function called in_array: http://us4.php.net/manual/en/function.in-array.php

So you could do in_array(1,$mystr);
It returns boolean so you could do:

Code:
if(in_array(1,$mystr)) {

} else {

}
If you use the third parameter it wont work unless you do '1' because the strict parameter compares the type.

Let me know if that helps.
Reply With Quote
  #6 (permalink)  
Old 05-26-05, 02:25 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$okvar 1;

echo 
in_array($okvarexplode(',''1,2,3')) ? 'OK' 'ERROR'
This should work, if I understand your problem correctly.
Reply With Quote
  #7 (permalink)  
Old 05-26-05, 02:29 PM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
YES, Almost, but it makes Loops.

outputs:
---------------------

OK
ERROR
ERROR

---------------------

Quote:
Originally Posted by Jaffizzle
I kinda understand but at the same time i don't
Ill just assume

PHP Code:

 <?
 $mystr 
"1,2,3";
 
$str=explode(","$mystr);
 
 
$okvar 1;
 for(
$i=0$i count($str); $i++){
 if(
$okvar == $str[$i]){
 echo 
"OK";
 } else {
 echo 
"ERROR";
 }
 }
 
 
?>
Reply With Quote
  #8 (permalink)  
Old 05-26-05, 02:38 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
what if he has "1,2,3,55"

and $okvar was 5?

it would pass when it should fail

wheezy's method will work!

Last edited by Jaffizzle; 05-26-05 at 02:41 PM. Reason: oops forgot something
Reply With Quote
  #9 (permalink)  
Old 05-26-05, 02:44 PM
qie's Avatar
qie qie is offline
Newbie Coder
 
Join Date: May 2005
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
<?
$mystr = "1,2,3";
$okvar = 1;
if(in_array($okvar, $mystr)){
echo "works";
}else{
echo "error";
}
?>

Warning
: in_array() [function.in-array.html]: Wrong datatype for second argument in c:\www\test\test.php on line 4
error
Reply With Quote
  #10 (permalink)  
Old 05-26-05, 03:56 PM
shroder shroder is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Ahhh. You'll still need need to use an explode and put it into an array. Sorry about that.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Return String values from a object saved in an array mr_wazzup Everything Java 1 04-15-05 09:11 AM
ASP syntax error stuckonaproject ASP 8 12-14-04 10:30 AM
retrieving specific data from parsed query string grayfox Perl 1 04-04-04 11:38 AM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM


All times are GMT -5. The time now is 06:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.