Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Evaluating Strings...


In php, I have an array called "q". The elements are:

$q[0] = 2
$q[1] = 4
$q[2] = 5
$q[3] = 3

I'd like to be able to loop through this array via a While loop, and
echo the contents of these array elements back to the screen.

When I try something like:

for($i = 0; $i < 4; $i++) {
echo $q[i];
echo eval("$q[i]");
echo eval("$q[" . i . "]");
}

I get the "String" instead of the "Contents". For example, the output
is $q[0] instead of 2, $q[1] instead of 4, etc.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Evaluating A Concatenated Variable
I have a form with 10 select boxes. The boxes are named "select1", "select2", ..., "select10". On the action page, I would like to be able to use a "for" statement to loop through all 10, since the action taken on each is identical. However, I am unsure how I can evaluate the each variable dynamically. It's probably easier to show.

Page 1:
<form>
<select name="select1">
<option value="blah1">blah1
</select>
<select name="select2">
<option value="blah2">blah2
</select>
etc. etc. etc.
</form>

Action Page:
<?
for ($i=1;$i<=10;$i++) {
$tmpValue = $select . $i; <---- This is the question
}?>

In this case, for i=1, tmpValue becomes "select1", not the evaluation of this variable, or "blah1". How can I assign the value of the variable select1 into tmpValue while using this for loop?

Evaluating Php Tags/documents Outside Of Web Server?
I need to generate and send multiple xml documents over a socket.

Is it feasible to use php as a templating language for these? How would
i go about that?

I figure i _could_ make them into 'normal pages' and fetch the contents
using some http library, but that doesn't sound too elegant. There has
to be a better way?

Evaluating Array And Skipping Certain Results
I'm having a brain freeze on this. I have a script where I am reading
the contents of a file into an array. I have that up and working with no
problem. Then I'm exploding the array using

while ($line = fgets($fd, 4096))
{
$arrayData = explode("-", trim($line));
}

Works fine. But what I want to do now is evaluate arrayData[2] to see if
it contains the integers "00" (that's zero zero) and if it does, I want
it to skip that entry. I know I need to set it up with
if arrayData[2] == "00" {

But I'm drawing a blank on how to get it to skip this entire line in the
final results. There may be multiple instances of this happening in each
file and I want to skip them all.

Coding To Detect Number Of Students That Evaluating A Person
i really in need of a php script that can ensure SYSTEM able to trace the number of people that log in into the system and evaluating person A and detect that number so that we will know whether person A has been evaluated by all her students?. This is to restrict the number of people evaluating a single person to the exact number of students only.

eg: person A teaches 10 students and when all her students evaluate her teaching, the system actually detects the number of student under her that already evaluate her; and if the number exceed, the system will detect 'has exceed the number of students'.

what if there are person A till Z? while, the number of students for each person is different?

My problem is, is there is a php code to detect the number of people that evaluating the same person (indicate as A) and already fill the quota for evaluating that person?

Getting A File Via Http Or Evaluating A Php File And Getting Its Output
I need to somehow merge two separate web programs written in php
together (from the user's perspective). This solution won't be
perminant, I just need something done quickly. I was thinking of two
different approches.

One would be to download a processed version (modified so it will
create validating html with the page it's being embeded in) of one of
the programs with the other, then just use print to embed it in the
page. I couldn't find out how to get a file via http here
(http://us2.php.net/manual/en/index.php). Sorry if I missed this in
the docs.

The other would be to process the other program and get it's command
line output, and print that.

If there's a better way to this, I'd be glad to hear it :)

Also, I've never written any php before; so sorry if I missed
something obvious to someone who has.

Strings And {...}
Is there something build into php where {0} or {1} means something?

For example,

SELECT * FROM table1 WHERE col1 = {0} AND col2 = {1};

Strings
in my db i have dates stored like dd/mm/yyyy. how can i split the string up to get date = dd month = mm and year = yy. This is so i can then get an array on the month so it can show dd [month name] yyyy

Joining Two Strings Into ONE?
I have no idea on how to joining two strings into ONE.
eg.
$str1 = "Hello ";
$str2 = "World.";

I want to make a string
$str3 = "Hello World.";

Combining Strings
Say I wanted to take three string variables,

$one = "123"
$two = "abc"
$three = "easy as"

, and combine them into a single variable:

$combined = "easy as abc 123"

I've looked over the implode function, but I'm not sure how to arrange the statement..

$combined = implode("", $one, $two, $three)

Returns an error "Wrong parameter count for implode() in test.php3"

Preg_replace With Strings
I have the following text:

------------
<A HREF="http://www.webwater.de/single.php4?top=unscribe&sub=123-@f.gruber&langr=de">hier</A>
------------
now i want to replace this with:
------------
hier
------------
but "123-@f.gruber" is variable.
I tried this one, but is dosn't work:

$repf = "<A HREF="http://www.webwater.de/single.php4?top=unscribe&sub=";
$repl = "&langr=de">hier</A>";
$bodyde = preg_replace ("/$repf.*/$repl", "hier", $bodyde);

Sorting Strings!
Hi

I need to read a .txt file and sort the value, highest firs, lowest last.

The .txt file contains the following info on a different row:
altavista:joe:5
yahoo:eric:2
disney:dis:4

the firs thing i did was reading the first line and split the code by ":" so that
echo $referrer[0] //would give altavista
echo $referrer[1] //would give joe
echo $referrer[2] //would give 5

I want to sort these lines on the numbers(highest first, lowest last) and print this in a table. How can i do this?
so what i would like in a table is this

<table>
<tr><td>
altavista</td><td>5</td></tr>
<tr><td>
disney</td><td>4</td></tr>
<tr><td>
yahoo</td><td>3</td></tr>
</table>


Is this possible anyway?

Can Php Concat Strings?
Can php concat strings, it's not a fuction listed on the php site? what would the format be like, $output concat($str1, $srt2).

Query Strings
How do you get the value you pass from one page as part of the query string i.e

htttp://www.somesite.com/postage.pgp?newsid=2

How you you get the value of the variable in the next page, stumped.

Breaking Strings?
I have a string (actually 100's of these, but lets take one):

$string ="TI: Some journal title AU: SomeAuthor,-S; Smith,-A; Jones,-B SO: SomeSource. 2002 Jan 9; 287(2): 188; discussion 189-90 JN: Some Journal Title";

This needs to be turned into an array:

$entry = array(
'Some Item Title',
'SomeAuthor,-S; Smith,-A; Jones,-B',
'SomeSource. 2002 Jan 9; 287(2): 188; discussion 189-90',
'Some Journal Title');

As you can see, each array item should be defined by what comes after either/all of the following: 'TI:' 'AU:' 'SO:' 'JN:'

So, I need to be able to create a pattern that can look through the string and return everything in between TI: and AU:, then AU: and SO, etc.

for example:

eregi(???, $string, $entry);

I'm fairly new to this and can't seem to figure out the regex I need for the pattern. Is this even the way to go about it?

Prepare Strings For XML
I was just wondering if anyone was aware of any prebuilt functions that would prepare strings for 'well-formed' XML. I.E. There is obviously a lot of various characters that do not work with 'well-formed' XML (&,$, etc.). I am looking for a pair of functions like stripslashes|addslashes; urlencode|urldecode.

Two Equal Strings?
I was just checking php.net but couldn't find a function that will compare to strings.

Did I just overlook it? or do I have to write a function to compare the two stirngs in php?

If there is a function, which one is it?
If there isn't a function, could you give me an idea of the coding that would compare two strings?


Query Strings
I have got query stings up and working on my site. But there are some problems. How do I make the title of the page change with the diff. index.php?page=

Also, I wanted the query strings to act like a frame, so when clicked it only changes the content, not the whole page.

I am using this code:
[PHP
<?php

if ($page=="whatever") {
include ("whereat");
}

elseif ($page=="whatever") {
include ("whereat");
}

elseif ($page=="whatever") {
include ("whereat");
}

else {
include ("whereat");
}

?>
[/PHP]

Appending Strings
How would I build up a string so that i get

$variable1 dynamically which should be used in a sql query like:

$sql = mysql_query("INSERT INTO **** (var) VALUES('$variable1');

the problem is that i need to run this 30 times and therefore need to create the $variable1, $variable2 etc on the fly - my problem is I cant get it working with my strings. Im using $i as integer in a loop. Tried things like
'$variable.$i' this wont work since i get the value .1 inserted.

How To Get Strings With Spaces?
I have a Select pop-up with list of country names with a variable $Country. I pass this variable using the URL to the next page and it displays fine. When I have a link from page two to page three, the Variable is cut after a space.
(e.g) Page one I select "United States of America". Page 2 shows it ok. But, on page 3 this variable is only "United" the rest is gone. here is the link i use:

<a href="Response5.phtml?Country=$Country&day=$day&month=$month&year=$year">$day</a>

Query Strings
I've been working on a script for a while now and have run into a problem. I'm using a function to spread out my guestbook entries throughout a few pages, however, when I add a query string into the equation it doesn't want to work, what I'm guessing is that a query_string call won't work in a function as it will everywhere else. PHP Code:

Query Strings
I use query strings like this: mypage.php3?q1=query1&q2=query2

Then I can use them as variables: $q1 = query1 / $q2 = query2

This works fine. I have seen sites that do it like this: mypage.php3/q1=query1/q2=query2/
How?

Formatting Strings?
i have a row in one of my databases, and i use a timestamp to imput the current time into the row. Now, when i output the timestamp i get something like 20001218155634. I tried reading the php manual to help me on editing strings, but it wasn't of much help.

So does anybody know any websites with good php string manipulating tutorials? or can anybody show me how to format the timestamp?

Joining Strings
I must be doing something wrong, but I can't see it. I an trying to build a long string that is to be a mail message, but I keep getting an error. PHP Code:

Switching Strings?
I want to be able to switch a certain word in a string with one that is in an associated array( is that right?). Here is an example: PHP Code:

Strings To Lowercase
I want to convert a URL typed in /ANyCAsE/ to /lowercase/ - the following does work if the URL is lowercase already but not if mixed or upper case. What is incorrect?

<?php
$url_array=explode("/",$REQUEST_URI);
$url_category_type=$url_array[1];
$str = strtolower($url_category_type);
print $str;?>

Parsing $ In Strings
I've got a file that has $str in it. I read the file into a string ($str is initialized). my question is, whether $str will be parse (replaced by it's value). If not, how can achive this.

Form Strings
i have a form, and the php script should populate the form with the values selected from the db, but it doesnt. PHP Code:

Alphanumeric Strings
Is there a way to check if a string only contains letters or numbers, and not using the ctype_alnum function?

Strings And Conditionals..
trying to learn strings... I'm a bit mystifed about
something.. how do you test for conditions in strings? for example,
how would you do what in JavaScript (or Java) you do like this

if (email.indexOf('@') != -1) { // etc..

also how do you test for length of a string? I tried:

if (strlen($myName) == 7) {

but got error...

I have looked at sections dealing with conditionals in tutorials, but
don't see examles of this (also don't see functions like indexOf(),
etc.. under Strings here, http://us2.php.net/manual/en/ref.strings.php..

How Does PHP Store Strings?
I've been told that PHP strings are null terminated. If PHP strings
are in fact null terminated, I'm a bit puzzled how the following code
can work:

$filename = 'ucs2_file.txt'
$contents = file_get_contents($filename);
echo "Echoing file contents";
echo $contents."";

The HEX contents of the file are as follows:

FE FF 00 61 00 62 00 63 00 64

FE FF is the byte order mark, 00 61 is the character 'a' encoded in
UCS-2, 00 62 is the character 'b' encoded in UCS-2, and the next two
characters in the file are 'c' & 'd' (encoded in UCS-2) represented by
00 63 and 00 64 respectively.

So, file_get_contents reads the file contents into the string. So I'm
quite sure that after the function call, $contents points to an area
of memory that stores the bytes that were in the file; FE FF 00 61 00
62 00 63 00 64.

On the assumption that strings are null-terminated in PHP, I'm
guessing that the echo function is implemented something like (in
pseudo-code):

while($currentByte != NULL)
{
printByte($currentByte);
$currentByte = getNextByte($currentByte);
}

In otherwords, the NULL character tells the function when the end of
the string has been reached. So why, if strings are in fact null
terminated in PHP, does the echo function NOT stop when reaching the
third byte in the aformentioned byte sequence? How does it know that
there are more bytes in the string after the NULL byte? This behaviour
leads me to believe that strings in PHP are something more than just
null-terminated byte sequences in PHP.

Cropping Strings
Is there any way to crop a text string to a certain length? i.e. [fictional] crop ($string, 40); would crop to the frist 40 characters?

Formatting Strings
I am currently using sprintf to format a report from a MySQL database data. I was wondering if there is an easy way to format text besides using sprintf. See below for the example I need to recreate. If any lines exceed the 75 column the need to wrap to the next line and indent 24 spaces. PHP Code:

How To Connect 2 Strings Together With PHP
I know how to do it in Javascript but its not the same for PHP. What i am trying to do is connect 2 strings together. Like the string "index" and ".php". For javascript, id probably write it like this ...

name = "index";
name2 = ".php";
name3 = name + name2;

that would connect them and put them into one variable. I tried that with PHP but it didnt work.

Functions And Strings
when I have a function that calculates some stuff relating to google map data and then prints it out, I can get the function to pass this data back as a string rather than print it out. What I'm intending to do is have the function do the calculations and then pass the data back so that I can run a regular job to generate the data (i.e. cache it) and then just read this in each time the page is called rather than run the calculations each time. Code:

UTF-7 Encoded Strings
Is there a way to convert normal strings to UTF-7 encoded strings? ie.
where < is +ADw-, where > is +AD4-, etc.?

I was thinking that utf8_encode might encode something similar enough
to UTF-8 for my purposes, but upon using it, utf8_encode doesn't seem
to "encode" anything...

Strings Extraction!?
im using sockets and php-win.exe to make a fully functional WEB SERVER!!! "PHP SUPPORTED" I have created everything so far, and I need help with the fnctinonality.  so far I can acess the main page on both the local (1924) and remote (80) ports, but I need to be able to get the request: Code:

Strings And Arrays...?
<?php
$var1="abcdefghij";
for ($i=0;$i<strlen($var1);$i++) {
echo $var1[$i].'<br>';}?>

Using that code above... How would i select more than one letter at a time and put it into individual strings for example: If i used that code and wanted to be able to so this:

echo $var1[abc]; ( outputs 'abc' )
echo $var1[def]; ( outputs 'def' )
echo $var1[ghij]; ( outputs 'ghij' )

Query Strings
I am creating some custom php forums right now, and I need some help with query strings.

In the forums, when someone edit his own post, I set the following query string:
"address.com/topic.php?t=123&control=edit"

Which opens a text area so that the user can edit his topic. Now when the user edit the topic and submit the form, the address stays:
"address.com/topic.php?t=123&control=edit"

But I want it to change back to:
"address.com/topic.php?t=123"

without the 'control=edit' part.

How To Compare Three Strings?
Can anyone tell how i go about comparing three strings
to see if they are the same?

I'm building an Email list system, where users can upload
three of their friends Emails.

I want to check, to make sure they don't load up the same
Email three times.

I was thinking something like this, But  strcmp only compares
two strings. I need to compare three
strcmp (string $email1, string $email2, string $email3)

Splitting Strings
I am trying to make post code search and need to be able to break strings apart by character. I can do it by spaces and things using the explode() function, but I want to be able to get the first letter of the string only, the first and second only, first second and third etc.

Random Strings!
I want to know how I can run random strings set by me. Here is the code that I want to randomize.

<?php
$answer1 = "Yes";
$answer2 = "No";
$answer3 = "Maybe";
$answer4 = "I'm not sure";
$answer5 = "Possibly";
$answer6 = "For sure";
$answer7 = "Precisly";
$answer8 = "Why ask me? I'm just a computer.";
$answerRandom = rand($answer1, $answer8);
?>

Combining Strings
Here's the format:

$var1 = ""
$var2 = "123"
$var3 = "456"

$var1 = $var1 + var2;

Now that works fine.

But when I do that again somewhere else
$var1 = $var1 + $var3;

The numbers inside the $var1 get scrambled and don't end up in 123456.
What am I doing wrong?

Finding Sub Strings
I think i might need to use preg_replace. However I have never used it before and can't workout how to find the following.

This is my string..
Call Out Fee for 10:00 on 20/8/2007

What I need is for this string to be reduced to 10:00 and 20/8/2007. These values would preferably be in an array. Could someone give me a hand with this please.

Splitting Strings
so after spending nearly all day on this problem, could someone please point me in the right direction. I am working on some scripts to format crossword clues etc. If I have a clue like the one below:

In which ocean was the Mary Celeste found abandoned and adrift? <8>

I need to format this so there are a maximum of 18 characters per line, but not splitting any of the words, so I need to get it formatted like below

In which ocean was
the Mary Celeste
found abandoned
and adrift? <8>

So only break it apart where there are spaces at the previous space before the 18th character, I need to put in a marker character where the line returns are put for part of my script further into it, so the completely formatted text will look like below,

In which ocean was
§the Mary Celeste
§found abandoned
§and adrift? <8>

I need this script to work for all length clues which is why I'm getting in such a mess with it and going round in circles,

File For Storing Strings?
Can I store multiple strings in a file and read/write to it as opposed to using a SQL Database? Can anyone offer an example?

Only Alpahnumeric Strings Allowed
I want my users to give a username and in this there are only small letters (a..z), numbers (1..0) and the underscore (_) allowed. How can I check the input of the users to be valid?

Echoing Strings With Quotemarks?
I have a statement that gives me an error:

echo "<table><tr><td>Title</td><td>Author</td><td>Date added</td><td>Hits</td></tr><tr><td><a href="test">$arttitle</a></td><td>$artauthor (<a href=/"mailto:$artauthoremail/">)</td><td>$artcreationdate</td><td>$arthits</td></tr></table><br>";


Parse error: parse error, unexpected T_STRING, expecting ',' or '' in /home/hsphere/local/home/cmwong/singaporeanimenews.net/mm-article.php on line 299


backslashing the quotemarks continues giving the error. How might I get around this?

How To Treat Strings As Arrays?
$string = "some dumb text";
$var = explode(" ", $string);

Would make $var = array("some", "dumb", "text"); but explode doesn't work with "" or a NULL value. I want to break a string up into the individual letters.. How would I go about doing this?

Building GET Query Strings
I am currently building a sports web application and have run into a
design question.

In order to create a new coach record, there are two pieces of
information that are needed: 1) username 2) team_id

It seems to me that the best way to do this is to break the problem
into two parts. First, search for the username and append that to the
URL: http://www.mysite.com?username=john

Next, I would present the user with another search box to search for a
team and then add '&team_id=1' to the GET url. The final URL should
look something like

http://www.mysite.com?username=john...action=addcoach

Does anyone know of a good example out there for doing this in a
methodical and elegant fashion?


Copyright © 2005-08 www.BigResource.com, All rights reserved