简体   繁体   中英

Find/Replace part of text in PHP and convert to HTML

I have a large number of ASCII text files and am listing out the contents of each using the code below:

<?php
$file = $_GET['file'];
$orig = file_get_contents($file);
$a =htmlentities($orig);
echo $a;
?>

Some strings of text in each ASCII file are references to file names of other files and I'm trying to find and replace them with a Hyperlink to that file.

For example, a text file might be called "LAB_E143.txt" which looks like this:

LAB_E143:
        LDX   $#FF          ; load X with $FF
        JSR   LAB_E151      ; jump to this location

and what I'm trying to find & replace are references beginning with "LAB_" (eg LAB_E151 in the example above) so that it displays the text as a Hyperlink with a href of:

http:\\capture.php?file=lab_e151.txt

Clicking on that link will then display the contents of that particular text file and so on. All the references begin with "LAB_" followed by 4 variable characters .

I've tried str_replace but am struggling to parse the 4 variable characters each time.

Any help / pointers greatly appreciated

You should use Regex for such cases. As shudder mentioned, preg_replace_callback should be the best function to use for this purpose.

  1. Detect all references with the following Regex: /LAB_(?<id>\\S{4})/
  2. Write a function to replace the matches with the <a> tag

That's it.

$text = 'LAB_8435 Lorem ipsum dolor sit amet. LAB_8337 Amet.';

$formattedText = preg_replace_callback('/LAB_(?<id>\S{4})/',  function ($matches) {
    return '<a href="/capture.php?id='.$matches[1].'">'.$matches[0].'</a>';
}, $text);

echo $formattedText;

Warning : you want to display file from specific folder - make sure that user can't change the path with provided string (file whitelist, filename sanitization), because it would be possible to do some serious damage.

I suggest not giving a clue that link is directly connected with included file name. Instead /capture.php?file=lab_e151.txt you may have /capture.php?id=e151 and then something like this:

$id = isset($_GET['id']) ? $_GET['id'] : ''; //in php7: $id = $_GET['id'] ?? ''; 
if (!preg_match('/[0-9A-Za-z]{4}/', $id)) { die('Invalid link'); }
$file = 'lab_' . $id . '.txt';

//...

$convertToLink = function ($matches) {
    return '<a href="/capture.php?id=' . strtolower($matches[1]) . '">' . $matches[0] . '</a>';
};

$code = preg_replace_callback('/LAB_([0-9A-Za-z]{4})/', $convertToLink, $string);

echo '<pre>' . $code . '</pre>';

If those 4 chars are hex number then you may use this pattern instead: /LAB_([0-9A-Fa-f]{4})/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM