Dividing a String into parts [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Dividing a String into parts [SOLVED]

Post by tecktalkcm0391 »

I am having trouble remembering how to do this:

I have PHP printing this on a page:
1 - 2 - 3 - 4 - 5

I want another PHP page to "run" that page and get the HTML it outputs along with the numbers and then divide it by the " - " and delete the " - " all together. But I want it to do this for however long the string it so if its
1 - 2 - 3 - 4 - 5 - 6 - 7 it would do it 7 times and then put it in an array and then count the array and then print some stuff on that new page.

How would I do something like that?

Thanks,
Chris
Last edited by tecktalkcm0391 on Wed Jul 26, 2006 4:12 pm, edited 6 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Code: Select all

implode();
explode();
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

look at explode()

edit: damn it...nice job quick draw mcgraw! beat me to it!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

ok, well how could I get the contents of numbers.php to be read by display.php to be set to a string, which is exploded.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Show your code yo.
Pegleg Jackson wrote:edit: damn it...nice job quick draw mcgraw! beat me to it!
Yargh..
Last edited by Benjamin on Tue Jul 25, 2006 11:09 am, edited 1 time in total.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Code: Select all

<?php
$str = include("../game/Florida/fantasy5.php");
// (up) outputs 3 - 17 - 22 - 26 - 32 or what ever numbers they change every so often
$numbers = array();
$numbers = explode(' - ', $str, -1);
print_r($numbers);
?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I was hoping your code would give me a better idea of what your trying to do, but here goes..

Code: Select all

$numbers = explode(' - ', $str, -1);

$numbers = implode('.', $numbers);

echo '<a href="page.php?values="' . $numbers . '">ARG</a>';
and then in page.php

Code: Select all

$numbers = explode('.', $_GET['values']);
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

OK, well here is a better idea of what I am trying to do.. I have a script that is outputting numbers.... the a format of X - X - X - X - X and I want to extract each of the numbers by somehow getting the numbers from the page that generates them (numbers.php, which users never see or go to) putting them into a string on (page.php, which is what the users see) and then printing them for a parameter for a flash animation in the following.

Code: Select all

<param name="flashvars" value="num1=(NUMBER 1)&num2=(NUMBER 2)&num3=(NUMBER 3)&num4=(NUMBER 4)&num5=(NUMBER 5)">
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

untested..

Code: Select all

$numbers = explode('.', $_GET['values'])

$counter = 1;
$queryString = '?';

foreach ($numbers as $value)
{
    $queryString .= 'num' . $counter . '=' . $value . '&';
    $counter++;
}

$queryString = substr($queryString, 0, -1);

$paramString = '<param name="flashvars" value="' . $queryString . '">';
astions is closed till tomorrow.. gotta get some sleep :)
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Well could someone tell me how to "read" another file and get its contents.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a logical file_get_contents()... come on now, you aren't doing research anymore?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

feyd wrote:a logical file_get_contents()... come on now, you aren't doing research anymore?
Sorry. I've been doing the research I even found that, but I keep getting stuff that is wrong. At one time I was getting the PHP codes from the page in the source code.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

How would I do that anyways, if I do it like this:

Code: Select all

$sFile = file_get_contents("home/dir/numbers.php");
if($sFile==FALSE){
   print 'error';
}
print $sFile;
I get the PHP Codes in the source, and nothing on the page.

If I do

Code: Select all

$sFile = file_get_contents("http://site.com/dir/numbers.php");
if($sFile==FALSE){
   print 'error';
}
print $sFile;
I get
Warning: file_get_contents(http://site.com/dir/numbers.phphp) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in /home/test.php on line 3
error
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you want the code to run? include() it.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I want the code to run and then for its outputed info to be put to a variable I can use on a page of PHP.
Post Reply