Page 1 of 1

php array problem.

Posted: Sun Sep 05, 2010 6:08 am
by dentrite
i have to hide five characters from end of a variable called $url which stores url like this: http://piczasso.com/i/oqtb8ba.png%3C/a i have not to remove the same url or edit it in my database but have to print it till http://piczasso.com/i/oqtb8ba.png. for this i hace written this code(infact i did this in c++), which is not working please help!!:

Code: Select all

$url=$db_field['url'];
$count=0;
               for($i=0;$url[$i]!='\n';$i++)
               { 
                $count++;
              }

             $till=$count-5;
            for($i=0;$i<=$till;$i++) 
             {

                    echo $url[$i];
              }

Re: php array problem.

Posted: Sun Sep 05, 2010 6:21 am
by MindOverBody
You need to explode(make an array) your url variable with explode function first. Then you will be able to approach it as an array.

Code: Select all

$url= explode ( "" , $db_field['url'] );
for ( $i = 0 ; $i <= (count($url)-5); $i++ ) {
      echo $url[$i];
}
This can be also achived with regular expressions, but that's if you are searching for more elegant solution.

Re: php array problem.

Posted: Sun Sep 05, 2010 6:22 am
by StathisG
You can also try this:

Code: Select all

$url = 'http://pizassoasdasdasas.com/i/oqtb8ba.png%3C/a';
$url = substr($url, 0, -5);
echo $url;

Re: php array problem.

Posted: Sun Sep 05, 2010 6:50 am
by dentrite
thank you. it works. :)