PHP preg_split()没有捕获字符串中的分割

时间:2022-04-20 02:43:23

I'm trying to use a regex with preg_split to separate a url from a string:

我正在尝试使用带有preg_split的正则表达式来将字符串与字符串分开:

    $body = "blah blah blah http://localhost/tomato/veggie?=32";
    $regex = "(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)";
    $url = preg_split($regex, $body);

The resulting array is:

结果数组是:

    array(2) (
    [0] => (string) blah blah blah 
    [1] => (string))

I would like to return:

我想回复:

    array(2) (
    [0] => (string) blah blah blah 
    [1] => (string) http://localhost/tomato/veggie?=32)

Not sure what I'm doing wrong here...any advice will be appreciated.

不知道我在这里做错了什么...任何建议将不胜感激。

3 个解决方案

#1


3  

Try adding another set of brackets to capture the entire URL with an optional preg_split() parameter:

尝试添加另一组括号以使用可选的preg_split()参数捕获整个URL:

$regex = "((((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+))";
$url = preg_split($regex, $body, null, PREG_SPLIT_DELIM_CAPTURE);

Output:

array(5) {
  [0]=>
  string(15) "blah blah blah "
  [1]=>
  string(34) "http://localhost/tomato/veggie?=32"
  [2]=>
  string(7) "http://"
  [3]=>
  string(2) "ht"
  [4]=>
  string(0) ""
}

#2


1  

It's failing because you are splitting on a URL, not on a delimiter. The delimiter in this case is the "last space before ftp or http":

它失败了,因为你分裂的是URL而不是分隔符。在这种情况下,分隔符是“ftp或http之前的最后一个空格”:

$body = "blah blah blah http://localhost/tomato/veggie?=32";
$regex = '/\s+(?=(f|ht)tp:\/\/)/';
$url = preg_split($regex, $body);

To break down the regular expression:

要打破正则表达式:

\s+ - One or more spaces
(?=...) - Positive look-ahead (match stuff in this group, but don't consume it)
(f|ht)tp:\/\/ - ftp:// or http://

#3


0  

The first issue is that your regex is not delimited (i.e. not surrounded by slashes).

第一个问题是你的正则表达式没有分隔(即没有斜线包围)。

The second issue is that given the sample output you provided, you may want to look into using preg_match instead.

第二个问题是,根据您提供的示例输出,您可能希望查看使用preg_match。

Try this, see if it's what you want:

试试这个,看看它是不是你想要的:

$body = "blah blah blah http://localhost/tomato/veggie?=32";
$regex = "/^(.*?)((?:(?:f|ht)tps?:\/\/).+)/i";
preg_match($regex, $body, $url);
print_r($url);

#1


3  

Try adding another set of brackets to capture the entire URL with an optional preg_split() parameter:

尝试添加另一组括号以使用可选的preg_split()参数捕获整个URL:

$regex = "((((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+))";
$url = preg_split($regex, $body, null, PREG_SPLIT_DELIM_CAPTURE);

Output:

array(5) {
  [0]=>
  string(15) "blah blah blah "
  [1]=>
  string(34) "http://localhost/tomato/veggie?=32"
  [2]=>
  string(7) "http://"
  [3]=>
  string(2) "ht"
  [4]=>
  string(0) ""
}

#2


1  

It's failing because you are splitting on a URL, not on a delimiter. The delimiter in this case is the "last space before ftp or http":

它失败了,因为你分裂的是URL而不是分隔符。在这种情况下,分隔符是“ftp或http之前的最后一个空格”:

$body = "blah blah blah http://localhost/tomato/veggie?=32";
$regex = '/\s+(?=(f|ht)tp:\/\/)/';
$url = preg_split($regex, $body);

To break down the regular expression:

要打破正则表达式:

\s+ - One or more spaces
(?=...) - Positive look-ahead (match stuff in this group, but don't consume it)
(f|ht)tp:\/\/ - ftp:// or http://

#3


0  

The first issue is that your regex is not delimited (i.e. not surrounded by slashes).

第一个问题是你的正则表达式没有分隔(即没有斜线包围)。

The second issue is that given the sample output you provided, you may want to look into using preg_match instead.

第二个问题是,根据您提供的示例输出,您可能希望查看使用preg_match。

Try this, see if it's what you want:

试试这个,看看它是不是你想要的:

$body = "blah blah blah http://localhost/tomato/veggie?=32";
$regex = "/^(.*?)((?:(?:f|ht)tps?:\/\/).+)/i";
preg_match($regex, $body, $url);
print_r($url);