Cherry Posted November 22, 2009 Share Posted November 22, 2009 Hey there, I was wondering how I would parse a text file similar to this: "Advertisements" { "1" { "type" "S" "text" "Ad1" } "2" { "type" "S" "text" "Ad2" } "3" { "type" "S" "text" "Ad3" } } But only parse the ad number and the section after the word "text". For example, in the above document, it would parse: 1) Ad1 2) Ad2 3) Ad3 There are currently around 25 entries similar to that, but that could increase up to 40, if that makes any difference. Thanks Quote Link to comment Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 $text = <<<TEXT "Advertisements" { "1" { "type" "S" "text" "Ad1" } "2" { "type" "S" "text" "Ad2" } "3" { "type" "S" "text" "Ad3" } } TEXT; preg_match_all('~"text"\s+"([^"]*)~', $text, $matches); print_r($matches[1]); Output: Array ( [0] => Ad1 [1] => Ad2 [2] => Ad3 ) Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 Sorry for the nooby question, but how would I just load advertisements.txt, and also output it in a standard text form, not as an array? I tried $text = file(advertisements.txt); , but to no avail. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 file will load the file as an array, which is not what you want, so you'd be better off with file_get_contents. You can display the information like: $text = file_get_contents('advertisements.txt'); preg_match_all('~"text"\s+"([^"]*)~', $text, $matches); echo implode("<br />\n", $matches[1]); Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 Thanks, that works perfectly Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 Sorry for the double post, but is there a way to remove all instances of "\n" and replace with " ". I tried str_replace("\n", " ", $matches[1]); but it just didnt affect the output. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 I'm not sure what you mean. There's no line breaks (\n) in there besides the ones I added on: echo implode("<br />\n", $matches[1]); If you want to move that just replace the \n with ' ': echo implode("<br /> ", $matches[1]); By the way, the reason what you tried wouldn't effect $matches[1] is because, well for 2 reasons mostly: 1. $matches[1] is an array, not a string 2. str_replace doesn't take the string by reference so you would need to do something like: $str = str_replace($search, $replace, $str); Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 In the actual text document, there are text sections that have "\n" in them, and I would like to try to remove them. I tried to do <?php $text = file_get_contents('advertisements.txt'); preg_match_all('~"text"\s+"([^"]*)~', $text, $matches); $str = str_replace("\n", " ", $matches[1]); echo implode("<br />\n", $str); ?> But after reading your post, I realise that isnt going to work, so how else would I go about doing this? Quote Link to comment Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 Aren't the line breaks (\n) already taken out when we parse the file with preg_match_all? I have a feeling you're misunderstanding the problem and this is what you want: <?php $text = file_get_contents('advertisements.txt'); preg_match_all('~"text"\s+"([^"]*)~', $text, $matches); echo implode(" ", $matches[1]); ?> If you wanted to go through all the elements and replace any line breaks with a space (even though I'm pretty sure there are none), you could use array_map <?php $text = file_get_contents('advertisements.txt'); preg_match_all('~"text"\s+"([^"]*)~', $text, $matches); $arr = array_map(create_function('$t', 'return str_replace("\n", " ", $t);'), $matches[1]); echo implode("<br />\n", $arr); ?> Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 The actual advertisments file can be seen here: http://sammyservers.com/advertisements.txt . Take a look at some of the advertisements with "\n" inside the text section. What I want to do is remove the "\n" when its parsed by the php file. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 22, 2009 Share Posted November 22, 2009 I still don't see any \n's.. Even if there were any my last post included something that would remove them all. Example: $text = file_get_contents('advertisements.txt'); preg_match_all('~"text"\s+"([^"]*)~', $text, $matches); $arr = array_map(create_function('$t', 'return str_replace("\n", " ", $t);'), $matches[1]); echo implode("<br />\n", $arr); Would output: Our PayPal email has changed to jamesross_134@hotmail.com\nSend donations or admin payments to this new address. For double points, type !donate and then use the link printed to your console to make the payment. To see what your Steam ID is, simply type !steamid in chat. To easily donate for double points, or to purchase admin, type !donate in chat. Type !motd in chat to find out how to purchase admin, donate, or to re-read the server rules. Don't use exploits or bugs.\nDon't use offensive sprays.\nDon't swear at anyone.\nDon't block.\nDon't spam. Don't point farm or support point farming.\nNo sexism, no racism, or any other form of baseless hate. Type motd in chat to find out how to purchase admin, donate, or to re-read the server rules. Current map is {CURRENTMAP}, next map will be {SM_NEXTMAP} Admins: When pmuting/gagging a player, always provide a reason\nFor example sm_pmute bob 'whining all the time' Admins: Do NOT overwrite other admins decisions by un-banning/muting players who've been permamuted or banned by higher admins. Admins: If unsure about unmuting/unbanning, contact either Cherry or Indigo. Say !maprate to rate the current map and view current ratings! Join Penis's awesome Ventrilo server: 208.100.9.200 : 9209 Donators / VIPs now can set a custom player model! Type !model in chat to open the menu. Visit http://sammyservers.com for info about our servers, how to donate, buy admin and view a list of all the admins. Admins: Before touching anyone, \nuse sm_report <'player name'> <'reason in quotes'> on them first. Before thinking about buying admin, carefully read the motd and the website, and do not send money until you are approved. In need of an awesome spray? Contact Taters by adding Willy on Steam! Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 Sorry if I am being really stupid, but I still see "\n", for example, in the first line, after the email address. Quote Link to comment Share on other sites More sharing options...
cags Posted November 22, 2009 Share Posted November 22, 2009 The problem is it holds the characters backslash followed by n, not to be confused with the single character 'newline'. The simplest way of getting rid of them is simply str_replace. Before you had... str_replace("\n", " ", $matches[1]); ...the problem with that is because the \n is in double quotes it's parsed as a new line character. Using... str_replace('\n', " ", $matches[1]); ...should take it as a literal string. Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 Thank you very much, that works perfectly Quote Link to comment Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 [ot]Forgive me for intruding but is there any reason you're using that particular format for the text file? It strikes me that using something like JSON[1], YAML[2], INI[3], etc. would make life simpler than manually parsing the file with regular expressions. Currently, the file bears a passing resemblance to JSON. [*]http://json.org/ http://php.net/json [*]http://yaml.org/ [*]http://en.wikipedia.org/wiki/INI_file http://php.net/parse_ini_file [/ot] Quote Link to comment Share on other sites More sharing options...
Cherry Posted November 22, 2009 Author Share Posted November 22, 2009 I'm using that format as the file is read by a SourceMod plugin for a Source Dedicated Server, and that is the only format it can read the file in. I believe this type of file is called a KeyValues file. (http://developer.valvesoftware.com/wiki/KeyValues) Quote Link to comment Share on other sites More sharing options...
salathe Posted November 22, 2009 Share Posted November 22, 2009 Fair enough, that explains it. Quote Link to comment 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.