English 中文(简体)
preg_match必须以“/”结尾?
原标题:preg_match must end with "/"?

在下面的preg_match中,我将与两个静态字符串$url和$my_folder进行比较。。。

  $url = get_bloginfo( url )
//$url =  http://site.com 

  $my_folder = get_option( my_folder );
//$my_folder =  http://site.com/somefolder;

$my_folder字符串后面有斜杠时,我得到了匹配项

 http://somefolder/go/

但这并不能创造一个匹配。。。

 http://somefolder/go

然而,另一个问题是,这也符合。。。

 http://somefolder/gone

代码是。。。

$my_folder =  get_option( rseo_nofollow_folder );
if($my_folder !==  ) $my_folder = trim($my_folder, / );
$url = trim(get_bloginfo( url ), / );

preg_match_all( ~<a.*>~isU ,$content["post_content"],$matches);

for ( $i = 0; $i <= sizeof($matches[0]); $i++){
    if($my_folder !==  )
    {
    //HERES WHERE IM HAVING PROBLEMS

        if ( !preg_match(  ~nofollow~is ,$matches[0][$i]) 
            && (preg_match( ~  . $my_folder .  /?$~ , $matches[0][$i]) 
            || !preg_match(  ~ . $url . /?$~ ,$matches[0][$i])))
        {
            $result = trim($matches[0][$i],">");
            $result .=   rel="nofollow"> ;
            $content["post_content"] = str_replace($matches[0][$i], $result, $content["post_content"]);
        }
    }
    else
    {
    //THIS WORKS FINE, NO PROBLEMS HERE
        if ( !preg_match(  ~nofollow~is ,$matches[0][$i]) && (!preg_match(  ~ .$url. ~ ,$matches[0][$i]))) 
            {
            $result = trim($matches[0][$i],">");
            $result .=   rel="nofollow"> ;
            $content["post_content"] = str_replace($matches[0][$i], $result, $content["post_content"]);
            }
    }
}
return $content;
问题回答

<代码>~^http://somefolder/go(?:/|$)~

您需要首先删除尾部斜杠并添加/?在正则表达式的末尾

$my_folder = trim($my_folder, / );
$url = trim(get_bloginfo( url ), / );

if ( !preg_match(  ~nofollow~is ,$matches[0][$i]) 
    && (preg_match( ~  . $my_folder .  /?$~ , $matches[0][$i]) 
    || !preg_match(  ~ . $url . /?$~ ,$matches[0][$i])))

这是在黑暗中拍摄的,但请尝试:

preg_match(  /  . preg_quote( get_bloginfo( url ),  /  ) .  ?/ , $matches[0][$i] )

您可以使用您想要的任何字符来代替/字符。我猜测您使用的是wordpress,并猜测get_bloginfo(url)被规范化为始终有一个尾部斜杠。如果是这种情况,最后一个斜线将由<code>选择

如果字符串是固定字符串,则只应使用strstr()trpos()[/code>。

你的例子改写了:

if (!strstr($matches[0][$i], "nofollow") 
 and strstr($matches[0][$i], $my_folder) 
 or !strstr($matches[0][$i], $url)  )

strpos的工作原理类似,但需要额外的布尔值检查:

if (strpos($matches, "nofollow") === FALSE
 or strpos($matches, $my_folder) !== FALSE)




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...