Preg Callback Issue

问题描述

| 大伙伴... 我需要您的帮助..请让我知道为什么这段代码无法进入回调函数...
  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\'];
  }
    

解决方法

        因为您的
$pattern
$content
不符 用
var_export
\ѭ1精确查看您的实际正则表达式 仅当您的内容与正则表达式匹配时,才会执行回调。由于没有匹配项,因此没有理由执行回调函数。 您实际正则表达式的一部分:
([A-Za-z0-9\\\\/_\\\\.\\\\~\\\\:-]*?)(Array)
似乎不希望\“ Array \”潜入您的模式(两者)。