I'm trying to make an output but I'm not so sure what's the best way to go about it.

1.) Get variable.
2.) delete first character.
3.) delete last four characters.
4.) concatenate at end of url.

(jsmith.jpg = domain.com/smith)

anybody care to show me the best way to get this done?

the variable is:
$authorpic

thanks.

    1.) Get variable.

    $var = $_POST['var'];

    2.) delete first character.

    $var = substr($var, 1, strlen($var));

    3.) delete last four characters.

    $var = substr($var, 0, strlen($var)-4);

    concatenate at end of url.

    $url = $_SERVER['PHP_SELF'].'/'.$var;

    All together that would be:

    $var = substr($var, 1, strlen($var));
    $var = substr($var, 0, strlen($var)-4);
    $url = $_SERVER['PHP_SELF'].'/'.$var;

    Hope that helps.

    ~Brett

      So i've been playing with the code for a bit, but it something doesnt seem right.

      <?php  
      $searchlink = $_POST['$authorpic'];
      $searchlink = substr($searchlink, 1, strlen($searchlink)); $searchlink = substr($searchlink, 0, strlen($searchlink)-4); $searchlink = 'http://www.domain.com/'.$searchlink; echo "<a href=['$searchlink']>Find$authorname's Pictures!</a>"; ?>

      Can you tell what I might have done wrong? :evilgrin:

        Actually, you can combine the two [man]substr/man calls into one:

        $var = substr($str, 1, -4);

        Concerning your code, it looks like you just got the HTML wrong.
        You could try something along the lines of:

        $searchlink = substr($_POST['$authorpic'], 1, -4);
        echo '<a href="http://example.com/' . $searchlink . '">Find ' . $authorname . '\'s Pictures!</a>';

        Of course, the "http://example.com/" part may not even be required.

          You also got the name of the $_POSTed variable wrong:

          $_POST['authorpic']

            I'm starting to get more and more confused.
            I'm sure it's something really simple.

            <?php
            
            	  $searchlink = substr($_POST['authorpic'], 1, -4);
            
            echo '<a href="example.com/' . $searchlink . '" target="_blank">Find ' . $authorname . '\'s Pictures!</a>';
            
            	  ?>

            suggestions?
            echo $authorpic; = jsmith.jpg
            echo $searchlink; = [empty]

              What happens when you
              echo $_POST['authorpic'];
              ?

                I tried:

                echo $_POST['authorpic']; 

                Which would make the complete insert:

                <?php
                
                
                	  $searchlink = substr($_POST['authorpic'], 1, -4);
                
                echo '<a href="http://example.com/' . $searchlink . '" target="_blank">Find ' . $authorname . '\'s Pictures! (coming soon)</a>';
                		  echo $_POST['authorpic']; 
                
                	  ?>

                And nothing additional was displayed.

                  And nothing additional was displayed.

                  So you are trying to say that $_POST['authorpic'] is empty?

                  Where do you get this variable from? A form with the POST method, or the GET method?

                    Yes it's empty

                    laserlight wrote:

                    Where do you get this variable from?

                    POST was initially suggested by Brett on the first post.
                    I just tried 'echo $authorpic;' and I got the correct output (without correct concatenation)

                      Then your code would be:

                      $searchlink = substr($authorpic, 1, -4);
                      
                      echo '<a href="example.com/' . $searchlink . '" target="_blank">Find ' . $authorname . '\'s Pictures!</a>';

                      The reason I used $_POST['authorpic'] is because you said:

                      1.) Get variable

                      Normally when you "get" a variable, it would be done via $GET, $POST, $_REQUEST methods. Sorry for the confusion.

                      ~Brett

                        Brett that worked perfectly.
                        Thank you for your help, and don't let me forget laserlight and packet.

                        Just so I can learn this stuff while I'm doing it,
                        $searchlink creates a new variable (and obvioulsy there is no need to introduce it earlier)
                        substr is some function to mess with the variable.
                        ($authorpic) brings in the variable that we want to mess with
                        1, takes away the first letter
                        -4 takes away the last four letters

                        then output.

                        Would there be any instance when you might have more than just two numbers in the substr?
                        Or what would be another common use for substr?

                        Thanks again, it works perfect. Now I can connect my CMS to my photo gallery quite smoothly.

                        Thank you.

                          Would there be any instance when you might have more than just two numbers in the substr?
                          Or what would be another common use for substr?

                          It would be easier if you read the PHP Manual on [man]substr/man.

                            Write a Reply...