trim() / ltrim()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RTT
Forum Commoner
Posts: 38
Joined: Thu Jul 17, 2003 10:22 am
Location: Wolverhampton, UK
Contact:

trim() / ltrim()

Post by RTT »

I'm having a strange problem. I've made a script that turns a load of URLS submitted into a form as text into clickable links.

Included with this script is a feature which checks if the links are active or not with the use of fopen().

My problem is that as the links are extracted from the text, using the explode() function, separated by semi-colons, whitespace is left at the front of each resultant string, meaning that fopen() always fails due to the leading whitespace.

I have tried trim()ing and ltrim()ing each URL string, however it fails at stripping any leading whitespace.

I made another example to check my ltrim() useage, and still it doesn't want to strip the unwanted whitespace.

Code: Select all

<?php

if(!empty($_POST[text])) {

        trim($_POST[text]);
        echo '"'."$_POST[text]".'"';
}

?>
If $text is entered into the above string with leading spaces, it doesn't strip the spaces/whitespace at all... :?:

The PHP manual says that without using the 2nd paramater for the ltrim() function, it strips whitespace, new rows, tabs and all... however I just can't get it to work?

If anyone could shed any light on this problem I would be really grateful... 8)
Last edited by RTT on Mon Jul 28, 2003 8:14 am, edited 4 times in total.
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

Can I see your code?
RTT
Forum Commoner
Posts: 38
Joined: Thu Jul 17, 2003 10:22 am
Location: Wolverhampton, UK
Contact:

Post by RTT »

Yeah sure, sorry.

Code: Select all

<?php

if(isset($_POST[urls])) {

        $url = explode(";",$_POST[urls]);
        $i = 1;

        echo "<p>";

        foreach($url as $k) {
        
                if($k == '') {

                        echo "<!-- blank link, ignoring -->";

                } else {

                        echo "<!-- trying $k -->";
                        ltrim($k);
                        $fp = fopen($k, "r");
                        
                        if(!$fp) {
                        
                                echo "<br>Link $i is not working (<a href="$k">double check</a>)";

                        } else {
                        
                                echo "<br><a href="$k">Link $i</a>";
                        }

                }
        fclose($fp);
        $i++;
        }


}

?>
It is probably something daft that I have overlooked, but my first usage of trim functions..
Last edited by RTT on Tue Jul 22, 2003 7:28 am, edited 4 times in total.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

try this

Code: Select all

<?php 

if(!empty($_POST&#1111;text])) &#123; 

        $trimmed = trim($_POST&#1111;text]); 
        echo '"'."$trimmed".'"'; 
&#125; 

?>
RTT
Forum Commoner
Posts: 38
Joined: Thu Jul 17, 2003 10:22 am
Location: Wolverhampton, UK
Contact:

Post by RTT »

thanks wayne, that hadn't occured to me (d'oh..)

Sorted now, works great thanks :)

just added $k = ltrim($k); right under the foreach() statement :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Couple of general points:

One thing that you really should read is:
Why is $foo[bar] wrong?

Also:

Code: Select all

echo '"'."$trimmed".'"';
shouldn't have double quotes around the variable:

Code: Select all

echo '"'.$trimmed.'"';
is better.

Mac
Post Reply