Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:24:26+00:00 2026-06-16T02:24:26+00:00

I’m having trouble with a string converted from a file resulting in the same

  • 0

I’m having trouble with a string converted from a file resulting in the same as that string would behave if it was inputted directly:

Here is my test.html file:

<html>

<font class="editable">
This is editable section 1
</font>
<br><br><hr><br>
<font class="editable">
This is editable section 2
</font>

</html>

Here is my php file:

<?php

//RETURN ARRAY OF RESULTS FOUND BETWEEN START & END IN STRING
function returnStartEnd($string,$start,$end){ 
     preg_match_all('/' . preg_quote($start, '/') . '(.*?)'. preg_quote($end, '/').'/i', $string, $m); 
     $out = array(); 

     foreach($m[1] as $key => $value){ 
       $type = explode('::',$value); 
       if(sizeof($type)>1){ 
          if(!is_array($out[$type[0]])) 
             $out[$type[0]] = array(); 
          $out[$type[0]][] = $type[1]; 
       } else { 
          $out[] = $value; 
       } 
     } 
  return $out; 
};


// RETURN FILE CONTENTS AS A STRING
function readFileToVar($file){
  $fh = fopen($file,'r') or die($php_errormsg);
  $html = fread($fh,filesize($file));
  return $html;
  fclose($fh) or die($php_errormsg);
};

$file = 'test.html';
$html = readFileToVar($file);
// OR
//$html = '<html> <font class="editable"> This is editable section 1 </font><br><br><hr><br><font class="editable"> This is editable section 2 </font> </html>';
$go = 'editable">';
$stop = '<';

$arrayOfEditables = returnStartEnd($html,$go,$stop);
echo "<br>Result:<br>";
var_dump($arrayOfEditables);

?>

Note the commented out $html. It is the same as what should(?) be returned from the test.html file. When trying to run the function returnStartEnd(), it works as expected on the commented out string, but not on the string created from file, returning an empty array.

What am I missing? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:24:27+00:00Added an answer on June 16, 2026 at 2:24 am

    Problem:

    To me it is looking as if the regular expression is having trouble with the multiple lines. That appears to be the difference between the string that you passed in (bypassing the file_get_contents()) and the contents of the loaded file.

    Solution:

    Change the value of your regular express to allow for multiple lines:

    $expression = '/' . preg_quote($start, '/') . '([\w\s.]*?)'. preg_quote($end, '/') . '/im';
    

    This regular expression looks for the starting, and puts all values between that and the end into a character class. Then, at the end, I added the m modifier, which puts it into multi-line mode.

    According to my tests, both ways, this was what made it work for me:

    $html = <<<HTML
    <html>
    
    <font class="editable">
    This is editable section 1
    </font>
    <br><br><hr><br>
    <font class="editable">
    This is editable section 2
    </font>
    
    </html>
    HTML;
    
    $alternate = '<html><font class="editable">This is editable section 1</font><br><br><hr><br><font class="editable">This is editable section 2</font></html>';
    
    var_dump($html);
    $expression = '/' . preg_quote('editable">', '/') . '([\w\s.]*?)'. preg_quote('<', '/') . '/im';
    var_dump($expression);
    
    preg_match_all($expression, $html, $m);
    var_dump($m);
    
    preg_match_all($expression, $alternate, $m);
    var_dump($m);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.