如何从字符串中获取http?

时间:2017-01-18 13:37:31

标签: php regex

我有以下字符串:

"http://add.co" id="num_1"

如何从此字符串中仅获取"http://add.co"

我尝试使用模式:

^http:\/\/(+s)*

4 个答案:

答案 0 :(得分:2)

你可以使用这个几乎捕获你网址的正则表达式^"http:\/\/[^"]+(?=")

String : "http://add.co" id="num_1"
Matches : "http://add.co

您可以在比赛中追加最后"来修复它。也许有人可以编辑我的正则表达式以包含最后一个"

请参阅此处的示例:http://yourserver/try/foo%40bar/your_pass

答案 1 :(得分:1)

像这样:

1)explode()函数根据分隔符将字符串分解为数组。

2)。preg_replace()替换与定义的正则表达式匹配的字符串的包含。

$string = '"http://add.co" id="num_1"';
$array = explode(':',$string);
echo preg_replace('/\"/','',$array[0]);

输出:

http

$string = '"http://add.co" id="num_1"';
$array = explode(' ',$string);
echo preg_replace('/\"/','',$array[0]);

输出:

http://add.co

答案 2 :(得分:1)

@echo off
rem // Change to the working directory `C:\` once:
pushd "C:\" || exit /B 1 & rem/ ("C:\" is the root directory of drive "C:")
rem // Ensure `OldDir.txt` exists by appending nothing:
>> "OldDir.txt" rem/
rem // Create new log file `NewDir.txt`:
> "NewDir.txt" dir /B "C:\Spektra\Gelen"
rem // Process all newly added items in `NewDir`:
for /F "eol=| delims=" %%F in ('
    findstr /V /X /I /G:"OldDir.txt" "NewDir.txt"
') do (
    rem // Do whatever you want with each file in `%%F`:
    unzip "%%F" -d "C:\some\destination\folder"
)
rem // Move new log file onto old one, suppress report message:
> nul move /Y "NewDir.txt" "OldDir.txt"
rem // Restore previous working directory:
popd

O / P

http://add.co //在我的机器上测试

答案 3 :(得分:1)

有几种方法可以达到你想要的效果:

使用preg_match

$str = '"http://add.co" id="num_1"';
preg_match('/^"(.*?)"/', $str, $matches);
echo $matches[1];

str_replaceexplode

$str = '"http://add.co" id="num_1"';
$url = str_replace("\"", "", explode(" ", $str)[0]);
echo $url;