Page 1 of 1

trim() / ltrim()

Posted: Tue Jul 22, 2003 7:18 am
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)

Posted: Tue Jul 22, 2003 7:21 am
by m@ndio
Can I see your code?

Posted: Tue Jul 22, 2003 7:25 am
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..

Posted: Tue Jul 22, 2003 7:25 am
by Wayne
try this

Code: Select all

<?php 

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

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

?>

Posted: Tue Jul 22, 2003 9:17 am
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 :)

Posted: Fri Jul 25, 2003 8:09 am
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