簡體   English   中英

PHP正則表達式匹配第一次出現?

[英]PHP regular expression match first occurrences?

http://someurlhere.com|http://sometexturl.com|sometexthere

我想只提取在|之前的第一個網址 http://someurlhere.com

我寫了正則表達式

\bhttp(.*)\|\b

但它捕獲了所有出現的

Thanx幫助表示贊賞

制作.* ungreedy: .*?

\bhttp(.*?)\|\b

如果你想只在第一個|之后匹配 做:

\|http://(.*?)\|

有了這個,網址是$1

這不是一個確切的答案,但我會指出您的信息,以幫助您解決這個問題!

正則表達式:匹配第一次出現的字符

不需要正則表達式。 我看到你把它標記為PHP。

$boom = explode('|', 'http://someurlhere.com|sometexturl.com|sometexthere');
$url = $boom[0];
echo $url;

輸出:

http://someurlhere.com

http://codepad.org/xnW0FTfA

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM