如何删除php中的空格

sy5wg1nm  于 2023-01-08  发布在  PHP
关注(0)|答案(2)|浏览(113)

当我运行我的php代码时,它完美地显示了内容,但是在start之后和stop之前有一个空格。我该如何删除所有空格呢?我以为这就是我在trim中所做的呢?

<?php
function getURL($u){
    $ops = array(
      'http'=>array(
        'method'=>"GET",
        'header'=>"Accept: text/html\r\n" .
                  "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0\r\n"
      )
    );
    $co = stream_context_create($ops);
    $r = file_get_contents('http://' . $u, false, $co);
    return $r != false ? $r : "";

}
function GetStringBetween($string, $start, $finish){
    $string = " ".$string;
    $position = strpos($string, $start);
    if ($position == 0) return "";
    $position += strlen($start);
    $length = strpos($string, $finish, $position) - $position;
    return substr($string, $position, $length);
}
$grab = file_get_contents('myurl'); 
$m3u8 = file_get_contents( trim($grab) );
$stream = GetStringBetween($m3u8, 'start"', '#stop');

?>
start<?=$stream?>stop
gg58donl

gg58donl1#

您应该删除所有空白字符,如下所示:

start<?= trim($stream, "\r\n\t ")?>stop
9udxz4iz

9udxz4iz2#

如果只删除空格,请使用str_replace。如果是包含空格的空白字符,请使用tab ... preg_replace

$string = preg_replace('/\s+/','',$string);

相关问题