开发者

Php: array function with fopen/fread

开发者 https://www.devze.com 2023-04-06 22:07 出处:网络
I\'m new to php so please bear with me here. Its a rough example but lets say I have a file (which obviously does\'t work :), say 1.php, there I have

I'm new to php so please bear with me here. Its a rough example but lets say I have a file (which obviously does't work :), say 1.php, there I have

<?php 
function links1($l1, $l2){echo "<a href='". $l1 ."'><img src='". $l2 ."'/></a>";}
$ext_file = '2.php';
$v1 = fopen($ext_file, 'r');
$data = fread($v1, filesize($ext_file));
//i assume some piece of code has to go here?? Or maybe i need a new approach ...
?>

where I'm trying to basically开发者_运维百科 read 2.php where I have

links1("gg1", "1.jpg");
links1("gg2", "2.jpg");
etc...

so that when I open 1.php I would see 1.jpg 2.jpg etc etc... How to do that?

And if its possible, how should I modify the code that I could only put

gg1 1.jpg
gg2 2.jpg
gg3 3.jpg
gg4 4.jpg
....

and it would still work?

Thank you guys for help!


It is possible...

$data = file_get_contents('2.php');
$data = explode("\n", $data);
foreach($data as $string){
    list($l1, $l2) = explode(' ', $string);
    links1($l1, $l2);
}

If your line end of line is set to Windows and not UNIX they use "\r\n"...


You can simply include 2.php inside 1.php.

<?php 
function links1($l1, $l2){echo "<a href='". $l1 ."'><img src='". $l2 ."'/></a>";}
include "2.php";
?>

2.php should look like:

<?php
links1("gg1", "1.jpg");
links1("gg2", "2.jpg");

There are 4 different types of include:

  • include - include a file, don't error if the file exists (but it does throw a warning).
  • require - include a file, throw an error if the file does not exist.
  • include_once - include a file only if it has not been previously included.
  • require_once - include a file only if it has not been previously required.
0

精彩评论

暂无评论...
验证码 换一张
取 消