从回显中的字符串中删除空格和BR [英] Removing empty spaces and BR from string in echo

查看:82
本文介绍了从回显中的字符串中删除空格和BR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了下面的代码,但目前它生成了一个结果字符串,但是有大约40多个空格.

I got the below code but at the moment it generates a string of results but with about 40+ empty spaces.

$user_  = JFactory::getUser();
$db     = JFactory::getDBO();
$levels = JAccess::getAuthorisedViewLevels($user->id);
foreach($levels as $key => $level)
{
  $query  = 'SELECT title FROM #__pf_projects';
  $query .= ' WHERE access = ' . $level . " AND TRIM(title) != ''";
  $db->setQuery($query);
  $projectlist = $db->loadResult($query).'<br>';
  echo $projectlist;
}

起初我以为array_filter()在这里会很好,但是正如PatrickQ指出的那样,它是一个字符串,因此数组过滤器将无法工作. 然后,我根据不要惊慌"的答案改编了代码.您可以在上面看到经过修改的代码.

At first I thought that array_filter() would be good here but as PatrickQ points out it is a string so the array filter won't work. Then I adapted the code according to the answer from Don't Panic. This adapted code is what you can see above.

它现在返回这样的列表.

It returns now a list like this.

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
http://www.domain1.com
<br>
<br>
<br>
<br>
http://www.domain5.com
http://www.domain23.com
http://www.domain65.com
http://www.domain213.com
<br>
<br>
<br>
<br>
<br>
<br>

那么如何修改代码以仅获得这样的列表:

So how to adapt the code to just get a list like this:

http://www.domain1.com
http://www.domain5.com
http://www.domain23.com
http://www.domain65.com
http://www.domain213.com

当您将<br>更改为,时,列表将变为,,,,,,,,,,,http,,,,,,httphttphttphttp,,,,,,,< =我把它记的短了一些.

When you change the <br> into a , then the list becomes ,,,,,,,,,,,http,,,,,,httphttphttphttp,,,,,,, <= I wrote it down a bit shorter.

推荐答案

感谢大家的回答,我终于找到了一种获取结果的方法.我的方式不一定适合所有人.在我这方面有更多知识的人可能会做不同的事情.

Thanks to everyone's answers I finally figured out a way to get the results. My way is not necessarily the right way for everyone. And someone with more knowledge then me in this area would probably do it different.

基本上,我的答案是在div内输出每个结果的整个字符串.这将创建很多空的div,但这些不是由HTML生成的.但是,它们确实出现在检查器中.

Essentially my answer is outputting the entire string with each result inside a div. This will create a lot of empty divs but those are not generated by HTML. They do however show up in the Inspector.

我在脚本中使用的最终代码.

The final code that I use in my script.

$user_  = JFactory::getUser();
$db     = JFactory::getDBO();
$levels = JAccess::getAuthorisedViewLevels($user->id);
foreach($levels as $key => $level)
{
  $query  = 'SELECT title FROM #__pf_projects';
  $query .= ' WHERE access = ' . $level;
  $db->setQuery($query);
  $projectlist = '<div class="project">'.$db->loadResult($query).'</div>';
  echo $projectlist;
}

这现在给我一个这样的列表:

This is now giving me a list like this:

http://www.domain1.com
http://www.domain5.com
http://www.domain23.com
http://www.domain65.com
http://www.domain213.com

这篇关于从回显中的字符串中删除空格和BR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆