简体   繁体   English

PHP正则表达式匹配第一次出现?

[英]PHP regular expression match first occurrences?

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

i want to extract only the first url which is before the | 我想只提取在|之前的第一个网址 , http://someurlhere.com http://someurlhere.com

i have written regular expression 我写了正则表达式

\bhttp(.*)\|\b

but its capture all the occurences of | 但它捕获了所有出现的

Thanx help is appreciated Thanx帮助表示赞赏

Make the .* ungreedy : .*? 制作.* ungreedy: .*?

\bhttp(.*?)\|\b

If you want to match only after the first | 如果你想只在第一个|之后匹配 do: 做:

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

with this, the url is in $1 有了这个,网址是$1

This isn't a exact answer, but I will point you towards information that help you solve this issue! 这不是一个确切的答案,但我会指出您的信息,以帮助您解决这个问题!

Regex: matching up to the first occurrence of a character 正则表达式:匹配第一次出现的字符

No need for regex. 不需要正则表达式。 I see you tagged it PHP. 我看到你把它标记为PHP。

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

Output: 输出:

http://someurlhere.com

http://codepad.org/xnW0FTfA http://codepad.org/xnW0FTfA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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