Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898775
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898811
Share on other sites

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 {}

 

 

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898820
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898830
Share on other sites

$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

 

 

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898853
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898864
Share on other sites

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 />';
}

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-898865
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/170374-regular-expression/#findComment-899007
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.