任务是:解析html代码并在doc,xls等上的每个链接之后插入文件修改日期。
有功能,从文件中获取此日期:
function getFileModDate($file)
{
if (substr($file, 0, 1) == "/")
{
$file = substr($file, 1, strlen($file));
}
$date = "";
if(file_exists($file))
{
$date = filemtime($file);
$date = trim(nDate($date));
$date = TAGFILEMDSTART.$date.TAGFILEMDSTOP;
}
return $date;
}
还有一个解析html并调用getFileModDate的函数。
function docLinksToModDate($text)
{
if($text)
{
$text = preg_replace("/<a\s[^>]*href=\"([^\"]*.(doc|docx|xls|xlsx|pdf))\"\s(?!data-hide=\"true\")[^>]*>(.*)<\/a>/siU", "\\0<em> Last edit: getFileModDate('\\1')</em>", $text );
}
return $text;
}
但即使使用修饰符 e 或preg_replace_callback()也无效。
有没有人知道在这种情况下该怎么做?
UPD:整个php代码
function docLinksToModDate($text)
{
if($text)
{
$text = preg_replace("/<a\s[^>]*href=\"([^\"]*.(doc|docx|xls|xlsx|pdf))\"\s(?!data-hide=\"true\")[^>]*>(.*)<\/a>/siU", "\\0<em> Last edit: getFileModDate('\\1')</em>", $text );
}
return $text;
}
function getFileModDate($file)
{
if (substr($file, 0, 1) == "/")
{
$file = substr($file, 1, strlen($file));
}
$date = "";
if(file_exists($file))
{
$date = filemtime($file);
$date = trim(nDate($date));
$date = TAGFILEMDSTART.$date.TAGFILEMDSTOP;
}
return $date;
}
$page_text = '<p>Sample text in Russian</p>
<h2>List of documents</h2>
<ul>
<li><a class="hello" href="http://www.td-vizavi.ru/docs/july_2014_jumping.doc" target="_blank">Document 1</a></li>
<li>Document 2 without file</li>
<li>Document 3 without file</li>
</ul>
<p><a href="/download/p_comp_fond_po.pdf" target="_blank">Request sample</a></p>
<p>Description of our services. Collaboration is crucial in creative ventures, yet building a culture that allows it to flourish can be tricky—particularly in traditional, hierarchically minded organizations. But with a little tweaking, any space has the potential to become a hotbed of connected thinking. As Rosie Manning learned recently, true collaboration thrives in an environment built on trust, openness, and flexibility.</p>
<p><a href="http://www.td-vizavi.ru/docs/june_person_2014_jumping.xls" data-hide="abracadabra">Letter sample</a></p>
<p>Related information</p>
<p><a href="http://www.td-vizavi.ru/docs/june_team_2014_jumping.xlsx" data-hide="true" target="_blank" >Document 4 </a></p>
<ul >
<li><a href="/newstext6.html">Link to web page</a></li>
<li> <a href="/about.html">Link to web page 2</a></li>
<li> <a href="/certificate.html">Link to web page 3</a></li>
</ul>';
$page_text = docLinksToModDate($page_text );
echo $page_text;
答案 0 :(得分:0)
使用preg_replace_callback
时,您有正确的想法,但了解回调的工作原理非常重要。该函数采用以下参数:preg_replace_callback($pattern, $callback, $subject)
(以及我们可以忽略的一些可选参数)。
来自php.net:
<强>回调强>
将调用并在主题字符串中传递匹配元素数组的回调。回调应该返回替换字符串。这是回调签名:
string handler ( array $matches )
换句话说,你不能做preg_replace_callback($pattern, 'my_function(\\1)', $subject)
之类的事情,但是你被迫将整个匹配数组传递给函数,你的回调必须得到正确的数组元素。
一个简单的例子:
// Returns: file.TXT
preg_replace_callback('/^.*\.([a-z]+)$/i', 'my_replacer', 'file.txt');
function my_replacer($matches) {
// Reference the matched group here, don't try
// to pass it from preg_replace_callback
return strtoupper($matches[1]);
}
$callback
可能只包含要调用的函数名称,因此如果您要将任何其他文本附加到替换文件中(例如&#34;上次修改:&#34;),您和# 39;我必须在$callback
本身做到这一点。