简体   繁体   中英

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

i have written regular expression

\bhttp(.*)\|\b

but its capture all the occurences of |

Thanx help is appreciated

Make the .* ungreedy : .*?

\bhttp(.*?)\|\b

If you want to match only after the first | do:

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

with this, the url is in $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.

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

Output:

http://someurlhere.com

http://codepad.org/xnW0FTfA

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