Skip to main content
Inspiring
May 7, 2019
Answered

Looking to create these 2 very specific (PHP-enhanced?) links

  • May 7, 2019
  • 2 replies
  • 378 views

I realize there are probably 50 better ways to achieve the same results, but a long list of limitations that are out of my control is forcing me to really narrow down the custom solution I'm looking for.

I'm looking to create these two very specific links :

  • An <a href=""> that targets the current/active url PLUS"_alt" in the filename
    (so "domain.com/about" becomes "domain.com/about_alt")
  • An <a href=""> that targets the current/active url MINUS the last 4 characters.
    (so "domain.com/about_alt" becomes "domain.com/about")

Every page on the site has an "_alt" version. I could insert the 1st link in the original template's navigation, and the other in the "alt" template, allowing users to easily bounce from either version of the page regardless of how many pages there are (without the use of databases or individual custom links). Even if there are thousands of pages, these 2 links would be all I need.

(The idea could be streamlined into a single link if the code had the ability to dynamically recognize whether the "_alt" is already present at the end of the current/active filename or not (adding it if it's not, removing it if it is). This would have the added benefit of allowing me to use 1 link across the site regardless of which version of the page I'm on (or template it's using). But this is more of a "nice to have" since even 2 links would solve pretty much everything.)

The site is currently plugged into jQuery and uses PHP (mostly for includes, I don't really know PHP beyond that). Additionally, thanks to you kind folks in this sub, I already have .htaccess stripping the extensions from the urls, so they needn't even be factored into the solution.

Am I being naïve believing 1 or 2 lines of PHP code in the <a> tag is all that might be needed here?

Thanks!

This topic has been closed for replies.
Correct answer Paul-M

Sounds like a strange set up ..... Why would you want to remove _alt if its present at the end of the link yet add it _alt if it is NOT present?

From what I can see you want something like?

<?php

//Define the current URL

    $myURL = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

//Define the string to append

    $myAppend = '_alt';

?>

echo current URL and append _alt

<a href="<?php echo $myURL . ' ' . $myAppend; ?>"> Link Text</a>

strip last for four characters from link

<a href="<?php echo substr($myURL, 0, -4); ?>">Link Text</a>

Example of detecting if the current URL contains '_alt' using PHP

<?php

//Get the current URL

    $myURL = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

   

    if (strpos($myURL, '_alt') !== false) {

    echo 'string contains _alt';

    } else {

     echo 'string does not contains _alt';

    }

?>

Difficult to understand why you want to do this, I think something is fundementally wrong, maybe you could address with something like mod_rewrite on an apache server.

2 replies

Paul-MCorrect answer
Legend
May 8, 2019

Sounds like a strange set up ..... Why would you want to remove _alt if its present at the end of the link yet add it _alt if it is NOT present?

From what I can see you want something like?

<?php

//Define the current URL

    $myURL = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

//Define the string to append

    $myAppend = '_alt';

?>

echo current URL and append _alt

<a href="<?php echo $myURL . ' ' . $myAppend; ?>"> Link Text</a>

strip last for four characters from link

<a href="<?php echo substr($myURL, 0, -4); ?>">Link Text</a>

Example of detecting if the current URL contains '_alt' using PHP

<?php

//Get the current URL

    $myURL = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

   

    if (strpos($myURL, '_alt') !== false) {

    echo 'string contains _alt';

    } else {

     echo 'string does not contains _alt';

    }

?>

Difficult to understand why you want to do this, I think something is fundementally wrong, maybe you could address with something like mod_rewrite on an apache server.

Paul-M - Community Expert
Nancy OShea
Community Expert
Community Expert
May 7, 2019

https://forums.adobe.com/people/Under+S.  wrote

Am I being naïve believing 1 or 2 lines of PHP code in the <a> tag is all that might be needed here?

Yes.  

Nancy O'Shea— Product User & Community Expert