Jump to content

How to parse certain lines of this text file?


Cherry

Recommended Posts

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

 

Link to comment
Share on other sites

$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
)

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

[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]

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.