SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert String to Link

    Hello,

    I am using a PHP function which spits out this format:

    27328, , File Name Title, , , http://www.domain.com/wp-content/upl...name-title.pdf

    I am looking for PHP code to simply parse this as a link in the syntax:

    Code:
    <a href="http://www.domain.com/wp-content/uploads/2020/11/file-name-title.pdf" >File Name Title</a>
    Any help would be much appreciated!

    thanks
    Andy

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is the input a csv file?

    Else if it is a string with just the amount of commas you showed us then this will do:

    PHP Code:
    $str "27328, , File Name Title, , , http://www.domain.com/wp-content/upl...name-title.pdf ";

    // use explode to break the parts into an array
    $bits explode(",",$str);

    // get rid of some the leading white spaces
    $bits array_map("trim" $bits);

    // access the array elements as needed
    echo "<a href='"$bits[5] . "'>" $bits[2]  . "</a>"
    Gives:
    <a href='http://www.domain.com/wp-content/upl...name-title.pdf'>File Name Title</a>

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups, this is a string so thanks for this. Is it possible to modify the function so that if the string is empty (i.e. user doesn't upload a file) to return the text "Not available".

    many thanks
    Andy

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The simplest way to write it is with an if conditional check.
    PHP Code:
    // comment out one line or the other to test this

    $str "27328, , File Name Title, , , http://www.domain.com/wp-content/upl...name-title.pdf ";
    //$str = "";

    if(trim($str) === ""){

      echo 
    "Not available";

    }else{

      
    $bits explode(",",$str);
      
    $bits array_map("trim" $bits);
      echo 
    "<a href='"$bits[5] . "'>" $bits[2]  . "</a>"

    You may then go on to check whether the string contains commas at all, or put it all into a function, or into a class and make it more robust.

    We can go on developing this idea to make it handle more possibilities.

    You may want to come up with a solution if for example the url is not actually a url, maybe it is missing the http://.... If the File Name Title is missing you may want to inject some default text, or send yourself an alert ...

    A lot will depend on how many places you can see yourself re-using this code, and the range of possible input strings or values -- but maybe this bit of code as it stands is all you need.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok here is my code but it has syntax errors... basically i just want to use a IF NOT statement to make things clean as this will be the only case.

    PHP Code:
    <?php $file get_field('fl_brochure' );if(trim($file) != ""){ echo '<a target=_blank href="'  $file['url' ] . '">'  '<img src="http://www.domain.com/images/brochure.png" alt="brochure" />' Brochure '</a>'?>
    Any help in fixing my code?!

    thanks so much
    Last edited by cpradio; Feb 16, 2013 at 13:18. Reason: changed [code] tags to [php] tags

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry the code that doesn't work is:

    PHP Code:
    <?php $file get_field('fl_map_id');if(trim($file) != ""){echo '<a class="colorboxiash" href="'http://www.domain.com/wp-content/plugins/leaflet-maps-marker/leaflet-fullscreen.php?marker=' . $file '">'  . '<img src="http://www.domain.com/images/map.png" alt="map" />' . Map . '</a>';} ?>
    Last edited by cpradio; Feb 16, 2013 at 13:18. Reason: Changed [code] tags to [php] tags

  7. #7
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where you are using echo, you need to escape all your inner single-quotes with a backslash.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    unfortunately this doesn't seem to fix the problem.. the url is just being read without converting $file to its value..

    The complication arises from the fact that the $file variable needs to be part of the url string...

  9. #9
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,398
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    1/ Put php code in php tags not code tags on the forum post.
    2/ The variable $file in ' ' will be $file but in " " will be the value of $file.
    3/ Try this:
    PHP Code:
    <?php
    $file 
    get_field('fl_map_id');
    if(
    trim($file) != ""){
    echo 
    "<a class=\"colorboxiash\" href=\"http://www.domain.com/wp-content/plugins/leaflet-maps-marker/leaflet-fullscreen.php?marker=$file\"><img src=\"http://www.domain.com/images/map.png\" alt=\"map\" />Map</a>";
    }
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •