How to remove spaces from my php

2019-09-12 05:16发布

When I run my php code it shows the content perfectly, but there's a space after start and before stop. How do I remove all spaces? I thought that's what I was doing with 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 

标签: php
2条回答
冷血范
2楼-- · 2019-09-12 05:34

You should trim all the whitespace characters, like this:

start<?= trim($stream, "\r\n\t ")?>stop
查看更多
你好瞎i
3楼-- · 2019-09-12 05:35

For removing only spaces, use str_replace. For whitespace characters which include space, tab... use preg_replace

$string = preg_replace('/\s+/','',$string);
查看更多
登录 后发表回答