简体   繁体   中英

Two questions regarding regular expressions

I currently use this:

$text = preg_replace('/' . $line . '/', '[x]\\0[/x]', $text);

$line is a simple regular expression:

https?://(?:.+?\.)?dailymotion\.com/video/[A-Za-z0-9]+

This is working fine so far. But there are two things that I need and I can't figure out, how to do that:

... I don't want to perform the replacement, if that string is contained within a BBCode ie

[bla]http://www.dailymotion.com/video/xuams9[/bla]

or

[bla=http://www.dailymotion.com/video/xuams9]trololo[/bla]

or

[bla=' http://www.dailymotion.com/video/xuams9 ']http://www.dailymotion.com/video/xuams9[/bla]

The 2nd thing is, that I just want to match until the first space. This is what I currently use:

$text = preg_replace('/' . $line . '(?:[^ ]+)?/', '[x]\\0[/x]', $text);

I don't know, if I should do it like this or if there's a better way.

So, basically i'm just trying to match

http://www.dailymotion.com/video/test4

from this:

[tagx='http://www.dailymotion.com/video/test1']http://www.dailymotion.com/video/test2[/tagx] | [tagy]Hello http://www.dailymotion.com/video/test3 World[/tagy] | [tagz]Hello World[/tagz] http://www.dailymotion.com/video/test4

EDIT:

This is, what i have so far (which works slightly):

(?:(?<!(\\[\\/url\\]|\\[\\/url=))(\\s|^))' . $line . '(?:[^ ]+)(?:(?<![[:punct:]])(\\s|\\.?$))?

You can use a lookbehind assertions to do this. http://php.net/manual/en/regexp.reference.assertions.php

By using the following lookbehind before $line

(?<!\\[bla]|\\[bla=|\\[bla=')

it will match $link that is not starting with [bla] , [bla= and [bla=' .

Try this:

$text = array();
$text[ 0 ] = "[bla]http://www.dailymotion.com/video/xuams9[/bla]";
$text[ 1 ] = "[bla=http://www.dailymotion.com/video/xuams9]trololo[/bla]";
$text[ 2 ] = "http://www.dailymotion.com/video/xuams9";
$text[ 3 ] = "A http://www.dailymotion.com/video/xuams9 B C";

$line = "/http:\/\/www.dailymotion\.com\/video\/[A-Za-z0-9]+/";

$tag = array();
$tag[ 0 ] = "/\[[A-Za-z]{1,12}\]http:\/\/www.dailymotion\.com\/video\/[A-Za-z0-9]+\[\/[A-Za-z]{1,12}\]/";
$tag[ 1 ] = "/\[[A-Za-z]{1,12}=http:\/\/www.dailymotion\.com\/video\/[A-Za-z0-9]+\][A-Za-z0-9]{0,}\[\/[A-Za-z]{1,12}\]/";

foreach( $text as $k=>$v ) {

    if( preg_match( $tag[ 0 ], $v ) == false && preg_match( $tag[ 1 ], $v ) == false ) {
        echo '!';
        $output = preg_replace( $line, '[x]\\0[/x]', $v );
    }
    else { $output = $v; };

    echo "Text #" . ( $k + 1 ) . ": {$output}<br />";

}

Result:

Text #1: [bla]http://www.dailymotion.com/video/xuams9[/bla]
Text #2: [bla=http://www.dailymotion.com/video/xuams9]trololo[/bla]
!Text #3: [x]http://www.dailymotion.com/video/xuams9[/x]
!Text #4: A [x]http://www.dailymotion.com/video/xuams9[/x] B C

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM