preg_match_all同一行上的多个匹配项

时间:2015-06-29 17:14:47

标签: php preg-match preg-match-all

我有一个脚本,可以为外部网址添加重定向页面

//Declarations
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!

@IBAction func loginButtonPressed(sender: UIButton) {
    let userEmail = usernameTextField.text
    let userPassword = passwordTextField.text

    //Check that both fields are filled
    if usernameTextField != "" && passwordTextField != "" {
        PFUser.logInWithUsernameInBackground(userEmail, password: userPassword) {
            (user: PFUser, error: NSError) -> Void in {
    }
}

https://eval.in/389389

当它比较不同行中的URL时,它会按预期添加重定向URL:

    // Do we have links?
    if (preg_match_all("/(\<a href=\")(.*)(\<\/a\>)/i", $message, $matches))
    {
        // Build local addresses and initialise replace arrays
        $localdomains = ($vbulletin->options['mh_anolink_localaddresses'] ? explode(" ", $vbulletin->options['mh_anolink_localaddresses']) : ($vbulletin->options['cookiedomain'] ? array($vbulletin->options['cookiedomain']) : array($_SERVER['SERVER_NAME'])));
        $preg_search = array();
        $preg_replace = array();
        $wb_list_checked = array();

        foreach ($matches[0] AS $key=>$fulllink)
        {
            preg_match("/(\<a href=\")(.[^\"]*)/i", $fulllink, $matches2);
            $serverurl = $matches2[2];

结果:

http://serverfault.com/
http://stackoverflow.com/

但是,当同一行中有多个网址时,重定向网址只会添加到第一个网址:

http://www.sampleurl.com/redirect-to/?http://serverfault.com/
http://www.sampleurl.com/redirect-to/?http://stackoverflow.com/

结果:

http://serverfault.com http://stackoverflow.com/

如何更换所有匹配项,无论新行如何?

谢谢。

0 个答案:

没有答案