View Full Version : Dumb Perl + PHP questions
WildComputer
01-05-2001, 01:26 AM
How to setup an array of strings....
I need basically just this for
- ASP/VBScript (I've done already)
- C (I know how to do this)
- C++ (I know how to do this)
- Perl
- PHP
- any other popular web server languages (suggest one, but only if you can give me the code).
The strings can contain any printable character (so which characters do I need to escape and how)
The array needs to start from 1 (so for languages with 0 based arrays, I add a dummy element at the start of the array.
I assume the Perl verson goes something like this, but don't know how to escape a ' if there's one in the string (plus is there anything else to address?)
@passwordlist=(
''
,'item1'
,'item2'
,'item3'
);
PHP?
Thanks
P.S.
In case you're wondering this is for a code generator program.
------------------
Over 207,000 Killer Domain Names (http://www.domaincavern.com/) from $11.97 each
Create Your Own High Profit E-Books (http://www.ebookcompiler.com)
Drive thousands of visitors to your web site (http://www.marketingrocket.com/buytraffic.html) cheap'n'easy
Affiliate Programs reviewed (http://www.hits4me.com/income.asp)
[This message has been edited by WildComputer (edited 01-05-2001).]
truelight
01-05-2001, 02:15 AM
I'm just a newbie in PHP, so I do not dare help you with the question.. Check out php.net and the manual - it should have the answers.
What I'm wondering thiugh, is what the heck you are going to use this form - I'm very curious!
MattC
01-06-2001, 06:30 PM
I don't quite understand the question, if you could please clarify some stuff I could help out easily http://geekvillage.com/ubb/smile.gif
Are the strings like this:
blahblahblah| |blahblah| |blah
and you want to make a PHP app to parse out the three "blah" strings based on the double pipe character?
If not.. you could use array() and parse a file into an array where every line is a new array entry:
blah
matt
joe
if that were the text file array(file.txt); then blah would be 0, joe would be 2 etc..
Post back or email me.
suresk
01-06-2001, 06:37 PM
In PHP, I've always created arrays like this:
$blah = array(
0 => "Value1",
1 => "Value2",
2 => "Value3",
etc..
);
I *Think* you can start with 1, but not sure..
as for escaping out apostrophes (and other quotes) just use the backslash \
HTH...
------------------
Spencer Uresk
spencer@uresk.net <A HREF="http://www.dailystarts.com" TARGET=_blank>
Daily Starts</A><A HREF="http://www.celebgrams.com" TARGET=_blank>
CelebGrams</A>
WildComputer
01-06-2001, 06:51 PM
The strings are constant. They can include any printable (spaces, punctuation,, quotes, numbers, upper/lower case letters, double quotes, slashes, etc., but not control characters like line feed or carriage return or nul or tab)
I just need a sample code, which
(a) allocates an array
(b) shows how to assign the elements going to n elements
(c) shows me any special cases (escape sequences etc.) that I need to worry about
EXAMPLE #1
in ASP/VBScript, strings are enclosed in double quotes, there are no magic escape sequences, so the special character I need to worry about is the double quote
so an ASP example would be
<%
DIM passwordlist(4)
passwordlist(1) = "aaa"
passwordlist(2) = "b"&CHR(34)&"b"
passwordlist(3) = "cc"&CHR(34)
passwordlist(4) = CHR(34)&"dd"
%>
where the strings are
aaa
b"b
cc"
"dd
EXAMPLE #2
in C, we use double quotes, arrays start from 0 (so need an extra element in the array), and \ must be escaped to \\, and " to \"
a C example would be
char passwordlist[4][] =
{
"",
"aaa",
"b\\b",
"c\"c"
** ;
where the strings are
aaa
b\b
c"c
EXAMPLE 3
I think that I have the Perl version now, using single quotes (correct me if I'm wrong, I am not good at Perl).
- use single quotes to avoid variable substitution
- need to escape ' to \' and \ to \\
so
@passwordlist=(
''
,'Hello'
,'O\'Reilly'
,'$test'
,'c:\\windows'
);
where the strings are
Hello
O'Reilly
$test
c:\windows
suresk (thanks!) has almost answered my question for PHP, I think, but I need a little more
- how to enclose the whole thing
- when _exactly_ do I use back slash
Is this _exactly_ right?
<?php
$passwordlist = array(
0 => "",
1 => "aaa",
2 => "b\\b",
3 => "c\'c",
5 => "d\"d",
6 => "ee\\",
7 => "ff\'",
8 => "gg\""
);
>
where the strings are
aaa
b\b
c'c
d"d
ee\
ff'
gg"
>> What I'm wondering thiugh, is what the heck you are going to use this form - I'm very curious!
All will be revealed by Jan 31
------------------
Over 207,000 Killer Domain Names (http://www.domaincavern.com/) from $11.97 each
Create Your Own High Profit E-Books (http://www.ebookcompiler.com)
Drive thousands of visitors to your web site (http://www.marketingrocket.com/buytraffic.html) cheap'n'easy
Affiliate Programs reviewed (http://www.hits4me.com/income.asp)
[This message has been edited by WildComputer (edited 01-06-2001).]
[This message has been edited by WildComputer (edited 01-06-2001).]
suresk
01-06-2001, 07:05 PM
I aplogize, the apostraphe does NOT need to be escaped, just the quotes ("). The Dollar Sign ($) is the only other one I know of. But you do need to take care that \n \t or \r don't wind up in there, or they will create problems (linefeed, carriage return and tab, respectively).
I made the one change to your code (took out the backslash in front of the ') and tried it out by echoing the array back. It worked fine, you can see it here:
http://spencedawg.dynip.com/array.php
here is the code I used:
---
<?php
$passwordlist = array(
1 => "aaa",
2 => "b\\b",
3 => "c'c",
5 => "d\"d",
6 => "ee\\",
7 => "ff\'",
8 => "gg\""
);
$i=1;
while ($i < 9){
echo "$passwordlist[$i]<br>";
$i++;
**
?>
---
------------------
Spencer Uresk
spencer@uresk.net <A HREF="http://www.dailystarts.com" TARGET=_blank>
Daily Starts</A><A HREF="http://www.celebgrams.com" TARGET=_blank>
CelebGrams</A>
WildComputer
01-06-2001, 07:49 PM
Thanks,
I missed line 4 in my original version, so I'll add it with a $, this version is right, right?
<?php
$passwordlist = array(
1 => "aaa",
2 => "b\\b",
3 => "c'c",
4 => "c\$c",
5 => "d\"d",
6 => "ee\\",
7 => "ff\'",
8 => "gg\""
);
$i=1;
while ($i < 9){
echo "$passwordlist[$i]<br>";
$i++;
**
?>
vBulletin® v3.7.4, Copyright ©2000-2013, Jelsoft Enterprises Ltd.