使用PHP回显没有空格的字符串
I have a foreach loop with list of links, and I'm trying to remove the spaces in the strings because I'll be using it as an href. So far, I successfully removed the spaces, but the code I wrote made a wide space before the string. Here's my foreach loop
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant): ?>
<li><span class="tenant"><a href="<?php echo $this->getURL() ?>catalogsearch/advanced/result/?tenants=<?php echo $ppmtenant['value'] ?>"><?php echo $ppmtenant['label'] ?></a><a href="<?php echo $this->getURL() ?>?___store=
<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>"></a></span></li>
<?php endforeach; ?>
</ul>
And below is what I did to remove the spaces. I used preg_replace. Now, the spaces are gone, but it has a wide space before the string.
<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>
Live example here: http://powerplantv2.jehzlau.net/brands. Screenshot here: http://i.imgur.com/wpQwxso.png
As you can see, if you hover on the images, there's an unwanted space after the ?. There should be no space so that my permalinks will work. If that space disappears, my problem will be solved. But I don't know how to make it disappear. I hope a php expert here can help me. :(
The problem is comming from the spaces in your html. To fix, simply move the variable declaration of $ppmtenantnospace
above the li output, then echo it in at the correct place:
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant):
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);?>
<li><span class="tenant"><a href="<?php echo $this->getURL() ?>catalogsearch/advanced/result/?tenants=<?php echo $ppmtenant['value'] ?>"><?php echo $ppmtenant['label'] ?></a><a href="<?php echo $this->getURL() ?>?___store=<?php echo $ppmtenantnospace;?>"></a></span></li>
<?php endforeach; ?>
</ul>
The whitespace is actually in your HTML code
You should open the php tag right after =
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant): ?>
<li><span class="tenant"><a href="<?php echo $this->getURL() ?>catalogsearch/advanced/result/?tenants=<?php echo $ppmtenant['value'] ?>"><?php echo $ppmtenant['label'] ?></a><a href="<?php echo $this->getURL() ?>?___store=<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>"></a></span></li>
<?php endforeach; ?>
</ul>
$spaceless_string = strtr($stringwithspaces, array(' ' => ''));
Try opening the PHP tag before the newline in your Code
<ul class="list grid effect-6" id="grid">
<?php foreach ($ppmtenants as $ppmtenant): ?>
<li><span class="tenant"><a href="<?php echo $this->getURL() ?>catalogsearch/advanced/result/?tenants=<?php echo $ppmtenant['value'] ?>"><?php echo $ppmtenant['label'] ?></a><a href="<?php echo $this->getURL() ?>?___store=<?php
$ppmtenantnospace = preg_replace('/\s+/','',$ppmtenant['label']);
echo strtolower($ppmtenantnospace);
?>"></a></span></li>
<?php endforeach; ?>
</ul>
have you tried to stick your <?php
to your ?___store=
? because the 4 spaces before the <?php
are not ignored since they are not PHP code but HTML output