DragonPrime - LoGD Resource Community
Welcome Guest
  • Good evening, Guest.
    Please log in, or register.
  • May 24, 2019, 06:59:34 PM
Home Forums News Downloads Login Register Advanced Search
* * *
DragonPrime Menu
Login
 
 
Resource Pages
Search

Pages: [1]   Go Down
  Print  
Author Topic: How To Get One Word Within A String Variable?  (Read 960 times)
0 Members and 1 Guest are viewing this topic.
TGTarheel
Mod God
*****
Offline Offline

Posts: 533


View Profile
« on: January 11, 2019, 01:49:03 AM »

OKO, I am stuck, what I am trying to do does not seem to be working right...and I did my research.  So can someone tell me what I am doing wrong?

What I am trying to do is to get the list of tattoos a person has with Petra's...and check if they have each tattoo.  This is not happening in Petra itself, but another module which is grabbing the pref "tatname" from the Petra module.  This is a string variable.  I had thought it an array at first, but when I used in_array function, I got a call stack informing me it was expecting an array and got a string.

So, I tried the strpos function.  My problem right now is that it always returns a true, even when it is in fact false (the requested thing is NOT in the string.

This is my coding at the moment
Code:
$rdtat = "no";
$tat = get_module_pref ("tatname","petra");
if (strpos($tat, "Magpie") !== false){
$rdtat = "yes";
} else {
$rdtat = "no";
}
if ($rdtat = "yes") {
output ("red");
} else {
output ("green");
}
So...what I am TRYING to do here...
I set $rdtat as "no" - so that it would always default to "no" when running
I grabbed the string variable which should contain the names of all the tats the player has. (PREF TATNAME, PETRA)

I am trying to compare the word "Magpie" against the elements of the string variable (this should come back as false in this case, I did that deliberately)

But it comes back as true, apparently, because the variable $rdtat - which I specifically set as "no" initially....is getting set to "yes"
because my output line says red instead of green  (I used red and green as samples, figuring green means go, I got it right, and red mean stop, I did not get it right)

Bottom line....what am I not doing right here?  It is finding something that is not in the string variable.

Kinda figured on...if I had got this far and got it right, I would then have changed "Magpie" to something it would find to test if that worked.  But I did not get that far.
Logged
TGTarheel
Mod God
*****
Offline Offline

Posts: 533


View Profile
« Reply #1 on: January 11, 2019, 01:54:47 AM »

Just for interest...I had it print out the value of $tat and this is what came back

a:9:{s:7:"mermaid";i:1;s:7:"rainbow";i:1;s:10:"red_dragon";i:1;s:13:"yellow_dragon";i:1;s:11:"blue_dragon";i:1;s:12:"white_dragon";i:1;s:12:"black_dragon";i:1;s:11:"gold_dragon";i:1;s:12:"green_dragon";i:1;}

This almost looks like allprefs to me.  But it isn't.

Still, though, should I actually do an unserialize on this?

But why would the strpos function find something that was not there?

How would I get where I am trying to go?
« Last Edit: January 11, 2019, 02:02:50 AM by TGTarheel » Logged
TGTarheel
Mod God
*****
Offline Offline

Posts: 533


View Profile
« Reply #2 on: January 11, 2019, 02:20:38 AM »

I tried an unserialize on this.  It still came back wrong, and this time it returned an array.
So I went back to in_array - still did not work.

So, I imploded the value of the fetched variable as a comma delineated string...and all I got was a bunch of 1's separated by commas.

I am truly flummoxed here.  Nothing is getting me to where I want to be.

I looked in the case: biostat of Petra.
But I don't understand quite what is happening there.  I want to compare each thing one at a time.
I need to compare ONE AT A TIME....each possible tattoo, do they have it or not?  And then do or do not do an action accordingly.

I am not sure how I get there.
« Last Edit: January 11, 2019, 02:25:36 AM by TGTarheel » Logged
Aeolus
Mod God
*****
Offline Offline

Posts: 1929


You're welcome.


View Profile WWW
« Reply #3 on: January 11, 2019, 02:23:50 AM »

Code:
if ($rdtat = "yes") {

Look at this line in your code. = is to set. == is to check. You're setting $rdtat to "yes" in your if statement.

Just for interest...I had it print out the value of $tat and this is what came back

a:9:{s:7:"mermaid";i:1;s:7:"rainbow";i:1;s:10:"red_dragon";i:1;s:13:"yellow_dragon";i:1;s:11:"blue_dragon";i:1;s:12:"white_dragon";i:1;s:12:"black_dragon";i:1;s:11:"gold_dragon";i:1;s:12:"green_dragon";i:1;}

Yes, that is a serialized array.

Still, though, should I actually do an unserialize on this?

How would I get where I am trying to go?

Use unserialize() on it, then you can use in_array() to see if a tattoo is in that array of tattoos.

But why would the strpos function find something that was not there?

Going back to the first part of my answer, that therefore means that strpos() is working fine. You're just re-setting the $rdtat variable after the use of strpos().
« Last Edit: January 11, 2019, 02:26:46 AM by Aeolus » Logged

Aeolus
Mod God
*****
Offline Offline

Posts: 1929


You're welcome.


View Profile WWW
« Reply #4 on: January 11, 2019, 02:27:28 AM »

I tried an unserialize on this.  It still came back wrong, and this time it returned an array.
So I went back to in_array - still did not work.

So, I imploded the value of the fetched variable as a comma delineated string...and all I got was a bunch of 1's separated by commas.

I am truly flummoxed here.  Nothing is getting me to where I want to be.

I looked in the case: biostat of Petra.
But I don't understand quite what is happening there.  I want to compare each thing one at a time.
I need to compare ONE AT A TIME....each possible tattoo, do they have it or not?  And then do or do not do an action accordingly.

I am not sure how I get there.

Go to http://phptester.net/ and put this in:

Code:
<?php
$s 
'a:9:{s:7:"mermaid";i:1;s:7:"rainbow";i:1;s:10:"red_dragon";i:1;s:13:"yellow_dragon";i:1;s:11:"blue_dragon";i:1;s:12:"white_dragon";i:1;s:12:"black_dragon";i:1;s:11:"gold_dragon";i:1;s:12:"green_dragon";i:1;}';
$t unserialize($s);
print 
"<pre>";
print_r($t);
print 
"<pre>";

It works. In your code, you might need to use stripslashes() first, when you get the pref.
« Last Edit: January 11, 2019, 02:30:54 AM by Aeolus » Logged

TGTarheel
Mod God
*****
Offline Offline

Posts: 533


View Profile
« Reply #5 on: January 11, 2019, 02:38:11 AM »

Thanks, Aeolus!

The = versus == thing did it.  Now I feel dumb, lol, I should have known better.

This was all I needed, the rest of it was overthinking the thing.  I just needed a check and a yes/no answer to feed an "if"

But good info to have for future reference, should I need it.  This is how I learn.  But I did look all this stuff up and read n it.
Logged
TGTarheel
Mod God
*****
Offline Offline

Posts: 533


View Profile
« Reply #6 on: January 11, 2019, 02:40:31 AM »

I tried an unserialize on this.  It still came back wrong, and this time it returned an array.
So I went back to in_array - still did not work.

So, I imploded the value of the fetched variable as a comma delineated string...and all I got was a bunch of 1's separated by commas.

I am truly flummoxed here.  Nothing is getting me to where I want to be.

I looked in the case: biostat of Petra.
But I don't understand quite what is happening there.  I want to compare each thing one at a time.
I need to compare ONE AT A TIME....each possible tattoo, do they have it or not?  And then do or do not do an action accordingly.

I am not sure how I get there.

Go to http://phptester.net/ and put this in:

Code:
<?php
$s 
'a:9:{s:7:"mermaid";i:1;s:7:"rainbow";i:1;s:10:"red_dragon";i:1;s:13:"yellow_dragon";i:1;s:11:"blue_dragon";i:1;s:12:"white_dragon";i:1;s:12:"black_dragon";i:1;s:11:"gold_dragon";i:1;s:12:"green_dragon";i:1;}';
$t unserialize($s);
print 
"<pre>";
print_r($t);
print 
"<pre>";

It works. In your code, you might need to use stripslashes() first, when you get the pref.

Overthinking for what I needed to do, but thanks.  I am bookmarking that phptester page!!
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


*
DragonPrime Notices
Please take the time to read the FAQ and browse the DragonPedia

Support Us
No funds raised yet this year
Your help is greatly appreciated!
Recent Topics
DragonPrime LoGD
Who's Online
123 Guests, 0 Users
Home Forums News Downloads Login Register Advanced Search