Page 1 of 1

How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 12:04 pm
by Namjies
I don't know if it can be done but I'd like an to create from an array a list of urls and the urls contain -, which gets interpreted as minus so the urls get calculated as letters minus letters which ends up showing all url as 0.html

Is there a way to ignore the minus sign in an array?

Thank you.

Re: How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 12:05 pm
by John Cartwright
str_replace()

Re: How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 1:45 pm
by requinix
Namjies wrote:I don't know if it can be done but I'd like an to create from an array a list of urls and the urls contain -, which gets interpreted as minus so the urls get calculated as letters minus letters which ends up showing all url as 0.html
...How are you turning a string with a URL into a mathematical expression? Why?

Re: How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 5:55 pm
by semlar
Yeah, strings don't work this way. It sounds like there's something wrong with the way you're storing the value.

Or, considering all of the URLs are returning the same value, maybe your output is being contaminated somehow.

Re: How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 7:13 pm
by Namjies
That should make it more clear with an example:

Code: Select all

 $games = array(submachine-0, submachine-1, submachine-2, submachine-3);
foreach ($games as $game){echo '
<div class="square"><a href="'.$game.'.html"><img src="http://example.com/games/'.$game.'.png" alt="'.$game.'" border="0" /><br/>'.$game.'</a></div>
';}
creates links to 0.html, -1.html, -2,html etc. values. I guessed the array interpreted the expression as letters without value and minus a number. So it actually works as long as there is no hyphen in the urls I want to create.

Simply for rendering games lists easily and changing the game folder regularly and easily to prevent leech of flash files. Might seem stupid but I'm creating a game website based on a few php lines instead of a premade script with a database. I find it useful to learn PHP. Already learned includes, a few server variables, variables, if and echo. I'm moving to arrays. But I couldn't find my answer on Google with this one.

Anyone can help with this? Thanks.

Re: How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 8:05 pm
by Eran
strings as array values/keys are the same as strings elsewhere - you need to wrap them with quotes.

Code: Select all

$games = array('submachine-0', 'submachine-1', 'submachine-2', 'submachine-3');

Re: How to ignore minus sign in arrays

Posted: Mon Feb 23, 2009 8:42 pm
by Namjies
ah, thank you so much! Easily fixed. Now I guess I have arrays creation added to my PHP knowledge.