php array problem.

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
dentrite
Forum Newbie
Posts: 16
Joined: Thu Jun 17, 2010 7:10 am

php array problem.

Post 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];
              }
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: php array problem.

Post 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.
Last edited by MindOverBody on Sun Sep 05, 2010 6:24 am, edited 1 time in total.
User avatar
StathisG
Forum Newbie
Posts: 14
Joined: Sat Mar 13, 2010 7:15 pm
Location: UK

Re: php array problem.

Post 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;
dentrite
Forum Newbie
Posts: 16
Joined: Thu Jun 17, 2010 7:10 am

Re: php array problem.

Post by dentrite »

thank you. it works. :)
Post Reply