-2

Trying to search a text file for a string and replace it with HTML code that is either an external file reference OR an anchor/bookmark within the same file.

Have added a diagram.

Blue Lines : If a file with the corresponding name exists, then replace with the HREF link to the file.

Red Lines : If a file doesn't exist AND the reference can be found in the local file, then it's a HREF link to the anchor/bookmark.

Thanks to Armin Šupuk for his previous answer which helped me out doing the Blue Lines (which works a treat!). However, am struggling to sort out the Red Lines. i.e. searching the local file for a corresponding link.

Amended Diagram

Finally, this is the path I've been heading down which fails to get a match at the Else If;

$file = $_GET['file'];
$file1 = "lab_" . strtolower($file)  . ".txt";
$orig = file_get_contents($file1);
$text = htmlentities($orig);
$pattern2 = '/LAB_(?<file>[0-9A-F]{4}):/';
$formattedText1 = preg_replace_callback($pattern, 'callback' , 
$formattedText);

function callback ($matches) {

if (file_exists(strtolower($matches[0]) . ".txt")) {
return '<a href="/display.php?file=' . strtolower($matches[1]) . '" 
style="text-decoration: none">' .$matches[0] . '</a>'; }

else if (preg_match($pattern2, $file, $matches))

{

return '<a href = "#LAB_' . $matches[1] . '">' . $matches[0] . '</a>'; }

else {
return 'LAB_' . $matches[1]; }
}

Current output diagram

TimJ
  • 27
  • 3
  • 1
    did you not ask the exact same thing some days ago? Some troubles? – Armin Šupuk Jul 17 '17 at 22:39
  • 1
    why aren't you using a database for this? – Funk Forty Niner Jul 17 '17 at 22:44
  • @Armin - similar but slightly different! – TimJ Jul 18 '17 at 09:38
  • @Fred -ii- Basically what I've got is a fairly lengthy assembler program in a single text file with jumps about. I'm trying to break it down so that PHP can read the text file and replace the parts where it jumps from sub-routine to sub-routine with a hyperlink to make it a lot easier to follow what is going on. – TimJ Jul 18 '17 at 09:41

1 Answers1

0

Some things:

  1. Try to write your code down in some usual format. Follow some code styling guide, at least your own. Make it coherent.
  2. Don't use variables with names like $formattedText1 or $pattern2. Name the difference.
  3. Use anonymous functions (Closures) instead of writing function declaration for functions which you go only to use once.

I renamed some variables to make it clearer what's going on and removed unnecessary stuff:

$fileId = $_GET['file'];
$fileContent = htmlentities(file_get_contents("lab_" . strtolower($fileId)  . ".txt"));

//add first the anchors
$formattedContent = preg_replace_callback('/LAB_(?<file>[0-9A-F]{4}):/', function ($matches) {
  return '<a href="#'.$matches[1].'">'.$matches[0].':</a>';
}, $fileContent);
//then replace the links
$formattedContent = preg_replace_callback('/LAB_(?<file>[0-9A-F]{4})/', function ($matches) {
  if (file_exists(strtolower($matches[0]) . ".txt")) {
    return '<a href="/display.php?file=' . strtolower($matches[1]) .
      '"style="text-decoration: none">' .$matches[0] . '</a>';
  } else if (preg_match('/LAB_' . $matches[1] . ':/', $formattedContent)) {
    return '<a href = "#LAB_' . $matches[1] . '">' . $matches[0] . '</a>';
  }  else {
    return 'LAB_' . $matches[1]; }
}, $formattedContent);

echo $formattedContent;

It should be clear what's going on.

Armin Šupuk
  • 809
  • 1
  • 9
  • 19
  • much appreciated. Understand the code a little better now - still struggling with the end result however. Have amended the diagram a little above to try and explain it a little better. The links to external files work as do marking the Bookmarks in the local file - but I'm still having problems with is trying to add links in each file to the bookmarks in the same file. – TimJ Jul 18 '17 at 20:31
  • It would help if you could edit your question with a part of the current result and then explaining with that example what's not right yet. I'm sorry, but you have a complex formatting issue and to fix it without the files which should be formatted is damn hard. – Armin Šupuk Jul 18 '17 at 22:08
  • Added new diagram to try and show current script output. Script does 3 things. Step (1) Search for everything in the file of the format LAB_xxxx: and turn it into a bookmark. This works. Step (2) is to look for any matches of the format LAB_xxxx for a filename of the same name on the web server and if found, create a HTML link to that file. This also works. Step (3) is to look for any remaining text in the format LAB_xxxx which wasn't picked up by Step (2) and turn it into a Hyperlink to a bookmark that was created in Step (1). This is the bit I'm struggling with. – TimJ Jul 18 '17 at 22:49
  • I'm not sure, but try to use `preg_match('/LAB_' . $matches[1] . ':/', $fileContent)` instead of `preg_match('/LAB_' . $matches[1] . ':/', $formattedContent)`. That's not a real fix, but some sort of monkey hack. I think the problem happens because we are manipulating the links earlier in step 1, so the pattern to find them ('/LAB_' . $matches[1] . ':/') isn't working anymore. Check the source code and ensure that the content of the new created links (in step 1) does still contain the whole string, including also the `:`. Maybe the `:` is outside of the tag? Please post one of these tags. – Armin Šupuk Jul 18 '17 at 23:12
  • I changed the code now a little bit. Maybe it will now work. – Armin Šupuk Jul 18 '17 at 23:25