0

伟大的伙伴......我需要你的帮助......请让我知道为什么这段代码没有进入回调函数......

  wppp_content_hook('<p><a href="http://localhost/work/Kate/wp-content/uploads/2011/05/amg_sls_01.jpg"><img width="300" height="250" class="alignleft size-medium wp-image-195" title="amg_sls_0" alt="" src="http://localhost/work/Kate/wp-content/uploads/2011/05/amg_sls_01-300x250.jpg"></a></p>');

  function wppp_content_hook($content) {

    $fileTypes        = array('\.bmp|\.gif|\.jpg|\.jpeg|\.png');
    $pattern          = array();
    $rel              = 'wp-prettyPhoto';
    $pattern[]        = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)>/i";
    $pattern[]        = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)(rel=('|\")".$rel."(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")".$rel."(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";


    return  preg_replace_callback($pattern,'_regexCallback',$content);
  }

  function _regexCallback($matches) {
   echo "<pre>";
    print_r($matches);
   echo "</pre>";  
    global $post;
    $fileTypes        = array('\.bmp|\.gif|\.jpg|\.jpeg|\.png');
    $rel            = 'wp-prettyPhoto';
    $pattern        = array();
    $replacement    = array();
    $pattern[]      = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)>/i";
    $pattern[]      = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)($fileTypes)('|\")(.*?)(rel=('|\")".$rel."(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")".$rel."(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
    $replacement[]  = '<a$1href=$2$3$4$5$6 rel="'.$rel.'[g'.$post->ID.']">';
    $replacement[]  = '<a$1href=$2$3$4$5$6$7>';

    return !strpos($matches['0'], '_blank')? preg_replace($pattern,$replacement,$matches['0']) : $matches['0'];
  }
4

1 回答 1

0

因为你的$pattern不匹配$content

var_export通过'ing精确查看您的实际正则表达式$pattern

只有当您的内容将被正则表达式匹配时,才会执行回调。否则没有理由执行回调函数,因为没有匹配项。

您实际正则表达式的一部分: ([A-Za-z0-9\\/_\\.\\~\\:-]*?)(Array)

似乎不需要的“阵列”潜入您的模式(两者)。

于 2011-05-30T14:51:00.473 回答