Reputation: 27
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_" (e.g. 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
Upvotes: 1
Views: 413
Reputation: 829
You should use Regex for such cases. As shudder mentioned, preg_replace_callback should be the best function to use for this purpose.
/LAB_(?<id>\S{4})/
<a>
tagThat'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;
Upvotes: 0
Reputation: 2101
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})/
Upvotes: 0