【问题标题】:preg_replace() find matches in MySQL Tablepreg_replace() 在 MySQL 表中查找匹配项
【发布时间】:2013-03-24 23:05:49
【问题描述】:
    function convText($v){

    $str = htmlentities($v);

    preg_match_all('(\[\[(.+?)\]\])', $str, $matches);

    foreach($matches['0'] as $match){

        $substring = substr($match, 1);
        $getData = bandToLink($substring);
        $str = preg_replace('/$match/', $getData , $str);
    }
    return $str;
    }

function bandToLink($v){

    $findBand = mysql_query("select * from news where news_title='". $v ."'") or die(mysql_error());
    if(mysql_num_rows($findBand)==0){
        return '<strong style="color:red">$v</strong>';
    } else {
        $getFind = mysql_fetch_assoc($findBand);
        return '<a href="/band/'. $getFind['seo_link'] .'.html">'. $getFind['news_title'] .'</a>';
    }

}

$text = "Lorem ipsum dolor sit amet, [[APPLE]] and Lorem [[CHERRY]] ipsum dolor";
echo convText($text);

如果在数据库中找到 APPLE,我如何将 [[APPLE]] 文本替换为 &lt;a href="#"&gt;APPLE&lt;/a&gt;?我在 ASP 函数上使用此模式进行相同的工作,但它不适用于 php。

【问题讨论】:

    标签: php mysql preg-replace preg-match


    【解决方案1】:

    您忘记在正则表达式中包含delimiters。应该是:

    preg_match_all('/(\[\[(.+?)\]\])/', $str, $matches);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-25
      • 2017-07-19
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多