blufish Posted August 4, 2008 Share Posted August 4, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/ Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 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!"; Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607418 Share on other sites More sharing options...
blufish Posted August 4, 2008 Author Share Posted August 4, 2008 When I ran this I got $txt = << And I don't really understand what this is supposed to do what does ([0-9]+)\ Do? Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607436 Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607442 Share on other sites More sharing options...
wildteen88 Posted August 4, 2008 Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607446 Share on other sites More sharing options...
blufish Posted August 4, 2008 Author Share Posted August 4, 2008 Still the same Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607448 Share on other sites More sharing options...
Stooney Posted August 4, 2008 Share Posted August 4, 2008 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 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607450 Share on other sites More sharing options...
wildteen88 Posted August 4, 2008 Share Posted August 4, 2008 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 = << Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607452 Share on other sites More sharing options...
blufish Posted August 4, 2008 Author Share Posted August 4, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607459 Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 It should match any string as long as it has this in it: width=200-height=150 Before you said you were grabbing it from a text file. Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607467 Share on other sites More sharing options...
blufish Posted August 4, 2008 Author Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607471 Share on other sites More sharing options...
Stooney Posted August 4, 2008 Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607472 Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 If that's the case then you would be better off going with the explode option that chrisdburns suggested. That way it'll grab all the variables necessary. Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607475 Share on other sites More sharing options...
blufish Posted August 4, 2008 Author Share Posted August 4, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607476 Share on other sites More sharing options...
wildteen88 Posted August 4, 2008 Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607482 Share on other sites More sharing options...
blufish Posted August 4, 2008 Author Share Posted August 4, 2008 Thanks this works great! Quote Link to comment https://forums.phpfreaks.com/topic/118073-solved-question-about-splitting-up-a-string/#findComment-607485 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.