2012-03-24 57 views
-4

我想知道最好的正則表達式替換此字符串與VID_ID它使用正則表達式預浸更換

$code = '&lt;object width=&quot;420&quot; height=&quot;345&quot;&gt;<br /> 
    &lt;param name=&quot;movie&quot; value=&quot;http://www.badoobook.com/clips/swf/player.swf&quot;&gt;&lt;/param&gt;<br /> 
    &lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;flashvars&quot; value=&quot;vid_id=100226&amp;MainURL=http%3A%2F%2Fwww.bado obook.com%2Fclips&amp;em=1&quot;&gt;<br /> 
    &lt;embed src=&quot;http://www.badoobook.com/clips/swf/player.swf&quot; flashvars=&quot;vid_id=100226&amp;MainURL=http%3A%2F%2Fwww. badoobook.com%2Fclips&amp;em=1&quot; type=&quot;application/x-shockwave-flash&quot; allowScriptAccess=&quot;always&quot; width=&quot;420&quot; height=&quot;345&quot; allowFullScreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;' 
$regular = ''; 
$code = preg_replace($regular ,'$1' , $code); 
echo $code; 

在這段代碼中的VID ID是

值= &quot; VID_ID = &amp;

感謝幫助

+0

你試過嗎? – piotrekkr 2012-03-24 21:07:49

+0

我所面對的大事是<我認爲有一些特殊的事情在監管表達來閱讀它,我不知道它 – 2012-03-24 21:10:06

回答

1

你可以使用正則表達式是這樣的:

(?<=vid_id=)\d+ 

這將使用positive lookbehind匹配任意數量的由文字字符集之前數字「VID_ID =」

這又是與使用RegexBuddy評論:

@" 
(?<=   # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) 
    vid_id=  # Match the characters 「vid_id=」 literally 
) 
\d   # Match a single digit 0..9 
    +    # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
" 

你已經用phpasp.net標記了你的問題,所以我不確定你在做什麼。這裏既是:

Asp.Net(C#)

Regex.Replace(@"...uot;vid_id=100226&amp;...", @"(?<=vid_id=)\d+", "[Your value here]") 

PHP

echo preg_replace("(?<=vid_id=)\d+", "[Your value here]", "...uot;vid_id=100226&amp;..."); 
2

使用正則表達式:

vid_id=(\d+) 
+0

這會給我的身份證,但我想要的是取代整個代碼與nember – 2012-03-24 21:20:04