tags:

views:

141

answers:

3

My HTML form:

<form action="http://localhost/wordpress" id="search" method="get">
<input type="text" size="30" id="s" name="s" value="Type and hit enter" onfocus="javascript:this.value='';" onblur="javascript:this.value='Type and hit enter';"/>
<br/>
<input type="submit" value="Search"/>
</form>

My PHP script:

<div class="message">
You searched for: 
<?php 
$searchterms = explode(" ", $_GET["s"]);
foreach ($searchterms as $searchterm) {
$length = strlen($searchterm);
$searchterm = substr($searchterm, 0, $length-1); ?>
<span class="searchterm"><?php echo $searchterm." "; ?></span>
<?php } ?>
</div>

It cuts off the last alphabet AND leave the darn space behind ><

+4  A: 

Why not just use the trim() function on every element of the exploded array?

clops
+3  A: 

Use trim(), or rtrim(), or ltrim().

yjerem
+5  A: 

Isn't it because you manually add a space when you echo $searchTerm? ;-)

Your code looks fine, I would just remove the substr, explode removes the whitespace between keywords.

If you really want to ensure you have no whitespaces around your keywords, you can use the trim function.

Vincent Robert
Oops my bad. I was experimenting beforehand >< Thanks!
Fabian