sandy1028 Posted August 15, 2009 Share Posted August 15, 2009 How to write a regular expression to read all the contents from {AUTHOR} to starting of another '{' {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/ Share on other sites More sharing options...
Michdd Posted August 15, 2009 Share Posted August 15, 2009 /\{AUTHOR\}(.+?)\{/ Should work. Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898741 Share on other sites More sharing options...
sandy1028 Posted August 15, 2009 Author Share Posted August 15, 2009 This doesnot work. Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898749 Share on other sites More sharing options...
wildteen88 Posted August 15, 2009 Share Posted August 15, 2009 Use {AUTHOR}([^{]+) Example: $text = '{AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS.'; preg_match('~{AUTHOR}([^{]+)~is', $text, $matches); echo nl2br(trim($matches[1])); Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898775 Share on other sites More sharing options...
sandy1028 Posted August 15, 2009 Author Share Posted August 15, 2009 <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); while(!feof($file)){ preg_match('~{AUTHOR}([^{]+)~is', $_, $matches); echo nl2br(trim($matches[1])); } Why the code hangs..... In welcome.txt, I have written the $text contents to file called welcome.txt Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898811 Share on other sites More sharing options...
.josh Posted August 15, 2009 Share Posted August 15, 2009 preg_match('~\{AUTHOR\}([^{]*)~is', $file, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898817 Share on other sites More sharing options...
sandy1028 Posted August 15, 2009 Author Share Posted August 15, 2009 The below code works fine if i write the $text in the same file. $text = '{AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. '; preg_match('~{AUTHOR}([^{]+)~is', $text, $matches); echo nl2br(trim($matches[1])); If I write the $text in a file called new.txt {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {AUTHOR} author2 staff2 {HEADLINE} and the below code doesnot print the same value as above <?php $fcontents = file ('new.txt'); while (list($line,$str) = each( $fcontents )) { preg_match('~{AUTHOR}([^{]+)~is', $str, $matches); echo nl2br(trim($matches[1])); } ?> It doesnot work and it doesnot print all the values between {AUTHOR} and first {} Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898820 Share on other sites More sharing options...
.josh Posted August 15, 2009 Share Posted August 15, 2009 that's because file grabs the contents of the file and creates an array where each line is an element. Which you then turn around and go through each line, but then try to preg_match as if it were the whole file, not the line. Just use file_get_contents and the regex. Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898830 Share on other sites More sharing options...
sandy1028 Posted August 15, 2009 Author Share Posted August 15, 2009 $fcontents = file_get_contents('welcome.txt'); preg_match_all('~{AUTHOR}([^{]+)~is', $fcontents, $matches); echo nl2br(trim($matches[1])); It prints an array. welcome.txt contains {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {AUTHOR} author2 staff2 {HEADLINE} How to just print the output of preg_match_all Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898853 Share on other sites More sharing options...
.josh Posted August 15, 2009 Share Posted August 15, 2009 it prints 'array' because preg_match_all returns an array. And you have more than one author to match. No offense, but if you would bother to take 2 seconds to read the manual entries on these functions, you wouldn't be asking these questions. I know you probably think you're giving it all you've got, but you've got to try harder, because if that's all you've got, you're going to constantly be struggling and maybe programming isn't for you. You can't echo an array, because an array is not the same as a variable. You have to either loop through the array and echo each individual element, or else dump it all out with something like print_r Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898864 Share on other sites More sharing options...
wildteen88 Posted August 15, 2009 Share Posted August 15, 2009 EDIT: CV beat me preg_match_all will return an array of matches ($matches). You'll have to loop through the array. preg_match_all('~{AUTHOR}([^{]+)~is', $fcontents, $matches); foreach($matches[1] as $match) { echo nl2br(trim($match)) . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898865 Share on other sites More sharing options...
sandy1028 Posted August 15, 2009 Author Share Posted August 15, 2009 <?php $fcontents = file_get_contents('file.txt'); preg_match_all('~{AUTHOR}([^{]+)~is', $fcontents, $matches); foreach($matches[1] as $match) { echo nl2br(trim($match)) . '<br />'; } ?> This works with the small file..... If the run the script on large file and redirect the to file gives out the message Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261125730 bytes) in filename.php Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-899007 Share on other sites More sharing options...
wildteen88 Posted August 15, 2009 Share Posted August 15, 2009 What is the large file? However just so you know why you're getting the error message your script is exceeding the maximum memory limit, which is 128MB. However you script is trying to use up to 250MB! Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-899015 Share on other sites More sharing options...
sandy1028 Posted August 16, 2009 Author Share Posted August 16, 2009 Is there any way to optimise the code and print the result to the file which doesnot consume more memory. Quote Link to comment https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-899440 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.