-1

这个功能是什么意思

function entre2v2($text,$marqueurDebutLien,$marqueurFinLien)

{

$ar0=explode($marqueurDebutLien, $text);
$ar1=explode($marqueurFinLien, $ar0[1]);
$ar=trim($ar1[0]);
return $ar;
}

上面代码中的 $text 是指文本文件的内容,它是通过以下代码从表单帖子中获取的:

$text=file_get_contents($_POST['file']);

任何人都可以描述一下提到的 php 函数吗?我不明白这两个变量是什么意思

$marqueurFinLien
$marqueurDebutLien

根据第一个答案,我试过了。但它显示错误。

Warning: Missing argument 3 for entre2v2(), called in C:\xampp\htdocs\php\test.php on line 5 and defined in C:\xampp\htdocs\php\test.php on line 13

Notice: Undefined variable: marqueurFinLien in C:\xampp\htdocs\php\test.php on line 18

Notice: Undefined offset: 1 in C:\xampp\htdocs\php\test.php on line 18

Warning: explode() [function.explode]: Empty delimiter in C:\xampp\htdocs\php\test.php on line 18

当我使用以下内容时:

<?

$text=file_get_contents('http://localhost/php/w.txt');

$name=entre2v2($text,"DB_USER', ',');");


echo($name);
echo("<br>");



function entre2v2($text,$marqueurDebutLien,$marqueurFinLien)

{

$ar0=explode($marqueurDebutLien, $text);
$ar1=explode($marqueurFinLien, $ar0[1]);
$ar=trim($ar1[0]);
return $ar;
}

?>
4

2 回答 2

3

In short, it looks like a way to parse a string - very inefficient and fragile, may I say.

$ar0 = explode($marqueurDebutLien, $text);

This line will break ('explode') a string stored in $text into array of substrings, using a $marqueurDebutLien string as a separator. Then it takes the second element of this array (i.e., what follows $marqueurDebutLien) and break it once more:

$ar1 = explode($marqueurFinLien, $ar0[1]);

... now using $marqueurFinLien as a separator.

The first element of this array is, by definition, the first string that lies in the original string ($text) between $marqueurDebutLien and $marqueurFinLien 'marker substrings'. Its trimmed version is what function returns.

Why inefficient, you may ask? There are actually several small things that could be quite easily improved here: limits for explodes, so only a small part would be processed, returning result of trim right away instead of storing it in some variable...

But in fact, the whole approach is flawed. Look at this:

function notSoMysticEntre2v2($text, $openingDelimiter, $closingDelimiter) {
   $mark1 = strpos($text, $openingDelimiter);
   if ($mark1 === FALSE) { 
       return null; 
   }  
   $mark2 = strpos($text, $closingDelimiter, $mark1);
   if ($mark2 === FALSE) {
       return null;
   }
   $data  = substr($text, $mark1 + 1, $mark2 - $mark1 - 1);
   return trim($data);
}

Take note that this function is quite more fool-proof than the first one: it will correctly fail to parse the string (returning null as a sign of error) if there's no opening delimiter there OR if it's not followed by closing delimiter.

于 2012-09-20T17:17:08.340 回答
1

该函数似乎从输入返回$marqueurDebutLien$marqueurFinLien字符串之间的文本$text

例如

$text = "abcdefghijklmnop";
$result = entre2v2($text, 'd', 'lmn'); // will return 'efghijk'
于 2012-09-20T16:55:32.377 回答