Jump to content

[SOLVED] question about splitting up a string


blufish

Recommended Posts

Okay so I have a text file with something like this,

name=fun-url=/stuff/funny.htm-width=200-height=150

name=boring-url=/stuff/boardgame.htm-width=500-height=700

And so on...

 

My question is if I was trying to find out the width for boring or something like that, how would I do it?

 

Thanks all!

 

 

Link to comment
Share on other sites

Regular expressions? Maybe.

$txt = <<<html
name=fun-url=/stuff/funny.htm-width=200-height=150
html;

preg_match("#width=([0-9]+)\-height=([0-9]+)#", $txt, $wh);
$width = $wh[1];
$height = $wh[2];

echo "Has a width of $width and a height of $height!";

Link to comment
Share on other sites

Did you just copy and paste that? It should work, did for me.

 

This:

([0-9]+)\

Just makes it check for only numbers 0 through to 9, the + sign means there must be one or more. So there has to be 1 or more for a match. The backslash escapes the - which is just after the backslash.

Link to comment
Share on other sites

When I ran this I got

$txt = <<

And I don't really understand what this is supposed to do what does

([0-9]+)\

Do?

 

If you're getting code outputted, then make sure you wrap the code in PHP tags (<?php ?>) and that your server is configured with PHP.

Link to comment
Share on other sites

You could use explode().

 

<?php
$var = 'name=fun-url=/stuff/funny.htm-width=200-height=150';
$var2=expode("-", $var);
foreach($var2 as $part){
    $var3[]=explode("=", $part);
}
?>

 

will produce:

Array
(
    [0] => Array
        (
            [0] => name
            [1] => fun
        )

    [1] => Array
        (
            [0] => url
            [1] => /stuff/funny.htm
        )

    [2] => Array
        (
            [0] => width
            [1] => 200
        )

    [3] => Array
        (
            [0] => height
            [1] => 150
        )

)

Link to comment
Share on other sites

When I ran this I got

$txt = <<

And I don't really understand what this is supposed to do what does

([0-9]+)\

Do?

 

 

Where are you putting the code? How are you running it? As I get the following output

Has a width of 200 and a height of 150!

 

Seems you may be doing something incorrectly, especially if displays $txt = <<

 

Link to comment
Share on other sites

Oops, I forgot to check for the <?php ?> tags, thank you for the answer, but how do I implement this so that I can get the information of the url and when I change a variable from fun to boring or whatever I can get the information relating to them.

 

Thank you!

Link to comment
Share on other sites

It should match any string as long as it has this in it:

Code:
width=200-height=150

Before you said you were grabbing it from a text file.

In the text file is the information for "fun" and "boring" I wish to have a variable and when I set it to "fun" it gets all of funs information and puts them into variables.

Link to comment
Share on other sites

Expanding on the explode() option:

 

edit 'echo $array['url'];' as to output whichever part you need.

 

<?php
$var = 'name=fun-url=/stuff/funny.htm-width=200-height=150';
$var2=explode("-", $var);
foreach($var2 as $part){
    $temp=explode("=", $part);
$array[$temp[0]]=$temp[1];
}
echo $array['url'];
?>

outputs:

/stuff/funny.htm

Link to comment
Share on other sites

I don't think I'm explaining what I need properly, I have a site and on this site there is a file called "games.php" and I use a get variable so it will be "http://www.frozenoven.com/games.php?name=fun", so then it opens up that folder and finds the "url", "width", and "height" that are on the same line as the name of fun.  If I set it to boring, it would find the "url", "width" and "height" that are on the same line as the name of boring.

Link to comment
Share on other sites

Try:

<?php

function getGames()
{
    $lines = array_map('trim', file('your_file.txt'));

    $search_word = 'boring';

    foreach($lines as $line)
    {
        $parts = explode('-', $line);

        list(,$name) = explode('=', array_shift($parts));

        foreach($parts as $p)
        {
            list($key, $value) = explode('=', $p);

            $game[$name][$key] = $value;
        }
    }

    return $game;
}

function findGame($game)
{
    global $games;

    if(isset($games[$game]))
    {
        return $games[$game];
    }
    else
    {
        return 'Game <b>' . $game . '</b> not found!';
    }
}

$games = getGames();
$game = findGame('test');

echo '<pre>' . print_r($game, true) . '</pre>';

?>

Note make sure you change your_file.txt to the name of the file that contains the list of games

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.