Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg_更换有问题吗?_Php_Preg Replace - Fatal编程技术网

Php preg_更换有问题吗?

Php preg_更换有问题吗?,php,preg-replace,Php,Preg Replace,在设法使用preg_match_all之后,我想用matchedText替换匹配的文本,但是它似乎不起作用。看看我的代码,我做错了什么 <?php $tweets = getTweets($profile->twitter_id); for($i=0;$i<4;$i++): // finds matches $num_matches = preg_match_all('/@\w+/', $tweets[$i]->d

在设法使用preg_match_all之后,我想用matchedText替换匹配的文本,但是它似乎不起作用。看看我的代码,我做错了什么

    <?php
    $tweets = getTweets($profile->twitter_id);

    for($i=0;$i<4;$i++):
        // finds matches
        $num_matches = preg_match_all('/@\w+/', $tweets[$i]->description, $matches);

        if($num_matches):
            echo $num_matches.'<br />';
            for($c=0;$c<$num_matches;$c++):

                $subject = $tweets[$i]->description;
                $pattern = array();
                $pattern[$c] = $matches[0][$c];
                $replacement = array();
                $replacement[$c] = '<a href="https://twitter.com/#!/">'.$matches[0][$c].'</a>';

            endfor;
                echo preg_replace($pattern, $replacement, $subject).'<br /><br />';

        else:
            echo auto_link($tweets[$i]->description).'<br /><br />';
        endif;
    endfor;
?>

也许,您的
preg\u replace
-ing不使用模式:
$matches[0][$c]
包含字符串,而不是带有分隔符的模式。再说一次,我可能错了。查看您的匹配项和替换项可能会有所帮助


Nips,我不敢相信我也忽略了循环中的数组声明。。。当然,这是你应该解决的第一件事

您需要在循环外部定义
$pattern
$replacement
,否则它们将在每次迭代时重新初始化为空数组:

$pattern = array(); $replacement = array();
for($c=0;$c<$num_matches;$c++):
$pattern=array()$替换=数组();

对于($c=0;$c谢谢大家的回答,但是我不再使用preg_replace,而是使用str_replace。它工作正常。这是我的最终代码

    <?php
        $tweets = getTweets($profile->twitter_id);

        for($i=0;$i<4;$i++):
            // first, explode the description to eliminate the username in the description
            $tweet_des = $tweets[$i]->description;
            $tweet_no_username_des = explode(':',$tweet_des,2); // added a limit parameter, ensuring only the username will be excluded
            $new_tweet_description = $tweet_no_username_des[1].'<br />';

            // the date the tweet is published
            echo $date_pub = $tweets[$i]->pubDate;

            // using preg_match_all to find matches, texts having '@' as the first letter
            $num_matches = preg_match_all('/@\w+/', $tweets[$i]->description, $matches);
            if($num_matches):   // if match(es) are found
                $search_arr = array();  // declared an array to contain the search parameter for str_replace
                $replace_arr = array(); // declared an array to contain the replace parameter for str_replace
                for($c=0;$c<$num_matches;$c++):
                    $search_arr[$c] = $matches[0][$c];
                    $name_links[$c] = explode('@',$search_arr[$c]);
                    $not_link_name = $name_links[$c][1];
                    $replace_arr[$c] = '<a href="http://twitter.com/#!/'.$not_link_name.'" target="_blank">'.$matches[0][$c].'</a>';
                endfor;
                echo auto_link(str_replace($search_arr, $replace_arr, $new_tweet_description)).'<br />';
            else:
                echo auto_link($new_tweet_description).'<br />';
            endif;
        endfor;
    ?>


起初,我的问题是找到以“@”开头的文本,并将其链接到Twitter上的相应帐户。请随意批评我的代码,我认为它仍然存在缺陷。:)

这是我第一次看到模板之外使用的语法。@mario,preg_match_callback()不存在。是的,我很愚蠢。它是
preg\u replace\u callback
使用那么你就不需要这个循环了。只需在回调中打包替换代码。(您的实际问题可能是缺少正则表达式转义/分隔符/除此之外,禁用了错误报告)完成了该操作,但它不会显示任何内容。我以为它会显示$subject,但它没有显示任何内容。