开发者

php regex to find what precedes a space

开发者 https://www.devze.com 2023-03-19 05:26 出处:网络
How to find a regex that can find me only the url\'s from those vars, basically there is a space always after the url.

How to find a regex that can find me only the url's from those vars,

basically there is a space always after the url.

$var1 = 'http://www.asdasgdst.com/thebook.html Result: chosen nickname "doodoo"; success (from first page)';

$var2 = 'http://adfhdfdfcvxn.com/blog/2008/12/28/beginnings/com开发者_C百科ment-page-1/#comment-8203 Result: chosen nickname "zvika"; success (from first page);';

$var3 = 'http://sdfsd.sdfjstestgg.com/453980.html?mode=reply Result: chosen nickname "sharon"; success; not working;';

$var4 = 'http://www.yuuyok.net/index.php?sub=community&site=posts&thread_id= Result: chosen nickname "kelly"; success (from first page);';


If all the string look like that then simply:

/^([^ ]*)/


You can just use explode for this, which will be much more efficient.

$url = array_pop(explode(" ", $var1, 1));


try with:

'/(^http.*?)\s/'

This should match all the strings that are starting with http until a whitespace is found


I think this will do the trick:

preg_match('~^(http://.*?) Result:~', $var1, $match);
$url = $match[1];


i'd go for:

preg_match('/(http:[^ ]+)/s', $var, $arr);
$url = $arr[0];

just in case the var isn't starting with http

besides, in order to test it you can try this regex (or any other):

(http:[^ ]+)

at: http://www.regextester.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消