在preg_Match()中添加文本和链接的问题

时间:2015-05-22 作者:TaghaBoy

我是PHP的新手,我使用这段PHP代码作为一种魅力(@BrianFegter):

add_action(\'template_redirect\', \'foobar_explode_if_no_citation\');
function foobar_explode_if_no_citation(){

    #Get the absolute server path to footer.php
    $footer_path = locate_template(\'footer.php\');

    #Store the footer file contents in a var
    $footer_contents = file_get_contents($footer_path);

    #The required string
    $citation_string = \'Designed by Foo Bar\';

    #Set off the nuclear bomb if there is an egregious offense
    if(!preg_match("/$citation_string/", $footer_contents))
        exit(\'All your websitez are belong to me. Make sure this string "Designed by Foo Bar" is included in footer.php\');
}
我尝试在这行中添加一些txt和链接

$citation_string = \'Designed by Foo Bar\';
未来

$citation_string = \'this is my txt and this is my url <a href="http://url.com">anchor</a>\';
但我有这个warning :

Warning: preg_match() [function.preg-match]: Unknown modifier \'/\'
谢谢你的帮助。

1 个回复
最合适的回答,由SO网友:TaghaBoy 整理而成

最后是@Rizier123提供的解决方案

if(!preg_match("/" . preg_quote($citation_string, "/") . "/", $footer_contents))
@Rizier123说:问题是你的图案中有斜线,只要使用preg_quote() 为了逃离他们,例如。

因此,代码如下所示:

add_action(\'template_redirect\', \'foobar_explode_if_no_citation\');
function foobar_explode_if_no_citation(){

#Get the absolute server path to footer.php
$footer_path = locate_template(\'footer.php\');

#Store the footer file contents in a var
$footer_contents = file_get_contents($footer_path);

#The required string
$citation_string = \'Designed by Foo Bar <a href="http://YourLink.com">anchor</a>\';

#Set off the nuclear bomb if there is an egregious offense
if(!preg_match("/" . preg_quote($citation_string, "/") . "/", $footer_contents))
    exit(\'All your websitez are belong to me. Make sure this string "Designed by Foo Bar" is included in footer.php\');
}

? Who will work with this code ?

编码人员worked days and night 提供精彩内容,并将引用词或链接作为版权。。不想让脚本的用户remove it or replace it 用他自己的without permission...

it\'s just a simple way to respect the copyright! respect coders.

结束