Jump to content

replace multiple characters with single character.


manicmoddin

Recommended Posts

Hi, I believe this is my first post on here. First off let me say thank you for silently helping out in the past while I keep on learning PHP.

 

I have decided to build an image gallery, and I am having problems with the page numbering links and the breadcrumbs.

 

Reson I decided to build my own is that I wanted a challange to learn more.

I could not find anything that would intregrate with the site as I wanted it to (pulled as a function so offers some customising with options).

 

The problem that I am having is that the <a href= tag is starting to give multipul "/"s when I only need one, so that the url at the top is coming up as

 

http://localhost/mmv4/gallery/pics///Ax/Ax_Jo/

 

when I am wanting http://localhost/mmv4/gallery/pics/Ax/Ax_Jo/IMG_4244.JPG.

 

I have used str_replace to catch //s and more, but this then adds odd slashes on the end so the page numbeing is getting odd results. some dirs have the / on the end and some do not, so the links are sometimes "visited" and others not.

 

I beleve that the problem is generated in

<?php
if(isset($_GET['album'])) {
      //load the available dirs into an array so that the sneeky users cannot get further back.
      $files = scandir($gal);
      $dirs = array();
      foreach($files as $file){
         if($file != '.' && $file != '..' && $file != 'thumbs') {
            if(is_dir($gal."/".$file)) {
               $dirs[] = $file;
               //echo $file ."<br />\n";
            }
         }
      }
      
      //if(in_array($_GET['album'],$dirs)) {
         $album = "/".$_GET['album']."/";
         $album = str_replace("..", "", $album);
      //}
      //else {
      //   $output .= "<p>There is no album called \"$_GET[album]\", please select an album from below.</p>";
      //}
   }

 

or the breadcrumbs bit

<?php
if($crumbs == 1) {
      //bread crums
       $path = $gal.$album;
       $path = explode("/", $path);
   
   
       $crumbs = array();
       foreach($path as $key => $value) {
          if($value != ""){
             $crumbs[] = $value;
          }
       }
       // create Breadcrumbs div
   
       $output .= "<div id=\"breadcrumbs\">";
       $number = count($crumbs);
       $real = "";
       foreach($crumbs as $num => $link) {
          if ($link == $gal) {
             $link = "Gallery Home";
          }
          if ($num == $number -1) {
             $output .= $link;
          }
          else{
             if ($num >= 1){
                $real ="";
                for($x=1; $x<=$num; $x++) {
                   $real .= "/".$crumbs[$x];
                }
                $output .= "<a href=\"?album=".str_replace("/", "",$real)."\">".$link."</a>"." <- ";
             }
             else{
                $output .= "<a href=\"?album=".str_replace("/", "",$real)."\">".$link."</a>"." <- ";
             }
          }
          $num +1;
         
       }
   
       //close breadcrumbs div
       $output .= "</div>";
   }

 

Can any one please help me shed some light on this one, I am confused.

I can send / attach the full source if needed.

 

MOD EDIT: tags fixed for syntax highlighting . . .

Link to comment
Share on other sites

The ability to edit is time-limited. Fixed the tags for you, though.

 

Cheers, thats clever.

 

I think I have found my error, but just for future reading, how do you go about this (the code above is irrelevant now). Ill give you an example. say I have a sting that has

 

php/////is/////////////////an//awesome///////language

 

how coulkd i take all of the extra "/"s out and just have

 

php/is/an/awesome/language

 

many thanks.

Link to comment
Share on other sites

In that situation since you have a potentially unknown number of the character you want to replace, a regex pattern would be the best way to handle it.

 

$string = "php/////is/////////////////an//awesome///////language";
$string =  preg_replace('~/{2,}~', '/', $string);
echo $string;

Link to comment
Share on other sites

In that situation since you have a potentially unknown number of the character you want to replace, a regex pattern would be the best way to handle it.

 

$string = "php/////is/////////////////an//awesome///////language";
$string =  preg_replace('~/{2,}~', '/', $string);
echo $string;

 

ahhh preg_replace

 

That scares me. nice one, will have a look into it on the other subforum.

 

Many thanks to the help there though, this has now been saved in my snippet database.

 

Gotta say this has been bugging me for a couple of days now.

 

Cheers - I saw a solved button a while back, but cant find it now. Will have to post more and learn the ropes.

 

Jimmy

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.