简体   繁体   English

从回显中的字符串中删除空格和BR

[英]Removing empty spaces and BR from string in echo

I got the below code but at the moment it generates a string of results but with about 40+ empty spaces. 我得到了以下代码,但此刻它生成了一个结果字符串,但有大约40多个空白空间。

$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;
}

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. 起初我以为array_filter()在这里会很好,但是正如PatrickQ指出的那样,它是一个字符串,因此数组过滤器将无法工作。 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

When you change the <br> into a , then the list becomes ,,,,,,,,,,,http,,,,,,httphttphttphttp,,,,,,, <= I wrote it down a bit shorter. 当您更改<br>进入,然后在列表变成,,,,,,,,,,,http,,,,,,httphttphttphttp,,,,,,, <=我写下来有点短。

If array_filter isn't filtering out empty values, then they probably aren't really empty. 如果array_filter没有过滤出空值,那么它们可能并不是真正的空值。 Assuming there is some sort of whitespace there rather than nulls or empty strings, you can probably modify your query to trim the title and only return results where there's still something there. 假设那里有某种空格,而不是null或空字符串,那么您可以修改查询以修剪标题,并且仅在仍有东西的地方返回结果。

SELECT title FROM #__pf_projects
WHERE access = ? AND title IS NOT NULL AND TRIM (title) != ''

Or, in terms of your original PHP code: 或者,根据您原始的PHP代码:

$query  = 'SELECT title FROM #__pf_projects';
$query .= ' WHERE access = ' . $level . " AND TITLE IS NOT NULL AND TRIM(title) != ''";

It is best to avoid concatenating variables into your SQL like this, though. 但是,最好避免像这样将变量连接到SQL中。 If the framework you're using has some way to utilize prepared statements, you should go that route instead. 如果您使用的框架具有某种利用预备语句的方法,则应改用该方法。


If this still doesn't work, I don't really know what else to try with the query, but you should be able to just check for an empty result in PHP and only echo if there's something to show. 如果这仍然不起作用,我真的不知道该查询还有什么尝试,但是您应该能够只检查PHP中的空结果,并且仅在有需要显示的情况下进行回显。

$projectlist = $db->loadResult($query);
if (trim($projectlist)) echo $projectlist.'<br>';

First thing, array_filter, if no callback was passed, will remove only falsy elements. 首先,如果未传递回调,则array_filter将仅删除虚假元素。 String with empty spaces is evaluated to true and therefore will not be remove from array. 空格为空的字符串计算为true,因此不会从数组中删除。 You can do something like: 您可以执行以下操作:

$filteredArray = array_filter($projectList, function($val) {
    return trim($val);
});

print_r($filteredArray);

Also, you can't echo an array. 另外,您无法回显数组。 You can use print_r or var_dump. 您可以使用print_r或var_dump。

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. 在我这方面有更多知识的人可能会做不同的事情。

Essentially my answer is outputting the entire string with each result inside a div. 本质上,我的答案是在div内输出每个结果的整个字符串。 This will create a lot of empty divs but those are not generated by HTML. 这将创建很多空的div,但这些不是由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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM