使用爆炸函数php,只在一行中回显字符


echo characters only in one line with explode function php

以下是我想要做的

我正在使用php爆炸函数,试图限制它在定义条件后打印的字符

{ 
      $result=http://php.net
              new line characters i don't want to print
       $links =explode("://",$result);
          $nows=$links[1];
            echo $nows;
}

正如你所看到的,上面的代码将打印

          php.net
          new line characters i don't want to print

但是我想在之后停止打印

php.net

您可以不使用任何内容替换换行符:

$nows = str_replace("'n", "", $links[1]);
$nows = str_replace("'r", "", $nows);
echo $nows;

如果你只想在第一行打印什么,试试这个:

$result = "php.net
           and some other text";
$nows = reset(explode("'n", str_replace("'r'n", "'n", $result)));

如果您要照顾的零件总是在第一行:

      $result="http://php.net
              new line characters i don't want to print";
       $links = explode("'n",$result);
/*
$links[0] ->http://php.net
$links[1] ->new line characters i don't want to print
*/
       $links =explode("://",$links[0]);
          $nows=$links[1];
            echo $nows;
/*
php.net
*/

无论如何,考虑提供更多关于你的案件的细节,以便提供更好的方式。例如,可能是regex?

尝试

$nows = trim( $links[1] );

TRIM()将删除换行符等

手动页面

编辑:现在我们有了实际情况,你说是:-

$result=http://php.net</br>nameserver:ns1</br>nameserver:ns2.

尝试

$t = explode( '</br>', $result );
$t1 = explode ( '://', $t[0] );
echo $t1[1];

请注意,如果是您在其他地方创建此字符串,则</br>不是有效的html标记,它应该是<br>,或者如果您使用XHTML,则它应该是<br />