Add php code to end of every html link - php

I have a query. I want to add a php code automatically to the end of every link on a page. For example, if I have 100 html links on a page for example:
<a href="http://example.com/" class="example">
and I wanted to automatically change this to:
<a href="http://example.com/<?php echo $ref; ?> class="example">
for every link on my page, how would I best implement it? Unfortunately, it is not an option to simply add the php code to every single link on my page. I've seen only one post related to this from Google that did not help.

If the inserting of the PHP code is a one-time task, you could use a "replace text" function of your editor.
You could have it replace each occurrence of
<a href="http://example.com/" class
by
<a href="http://example.com/<?php ... ?>" class
For example, have a look at Geany: https://askubuntu.com/questions/302914/find-and-replace-text-in-multiple-files-using-geany
If you have to do this often or automatically, you could use, for example, the preg_replace functions of php to process your input HTML files.

Javascript can help you doing this.
function renameLinks(){
var myList = document.getElementsByTagName("a");
for (var i =0;i<myList.length;i++){
myList[i].setAttribute('href', 'http://example.com/<?php echo $ref; ?>');
}
}
Create a function which takes a URL as parameter and it will generate your link:
function createLink(url){
var newLink = document.createElement('a');
newLink.href = 'url'+'<?php echo $ref; ?>';
return newLink;
}

sed -i 's/<a href="http:\/\/example\.com\/"/<a href="http:\/\/example\.com\/<\?php echo $ref; \?>"/g' *.php
Run this in terminal, it will replace all
<a href="http://example.com/"
with
<a href="http://example.com/<?php echo $ref; ?>"
in all the *.php files of current directory.
Update:
sed -i 's/old/new/g'
sed = Stream editor comment
-i = replace original file
s = the substitute command
old = a regular expression describing the word to replace
new = the text to replace it with
g = replace all occurrence
*.php = wildcard file name

Suggestion:
Since you have to modify all your code,
why not remove the hard-coding of http://example.com/ ?
With vim, you could use
%s/http:\/\/example.com\/<?php echo $domain . $ref;?>
you need to define $domain or use a constant:
define("MYDOMAIN", "http://example.com/");
php echo MYDOMAIN . $ref;
Then the replace (s as substitution) in vim would be
%s/http:\/\/example.com\/<?php echo MYDOMAIN . $ref;?>

Related

PHP echo $this->item->link in a href not working - showing JObject->link

quick question as I'm pulling my hear out!
In a PHP file I have different items pulled through such as contact details etc. I have pulled through $this->item->link which shows a URL which is perfect. I just want to make the url clickable, so I had the following code:
echo "<a href='$this->item->contactlink' target='new'>";
echo $this->item->contactlink;
echo '</a>';
This link doesn't work, stays within the site with a 404 and the URL looks like: http://domain.co.uk/JObject->contactlink
What can I do or have I missed to get the link clickable?
Kind regards
Liam
You're hitting a PHP parser glitch: It's not "greedy". Using a nested object reference, or multidimensional array, in a "-quoted string only parses the FIRST "dimension" of the object or array as part of the object/array:
php > class z { function __toString() { return get_class($this); } }
php > $x = new z();
php > $x->foo = new z();
php > $x->foo->bar = new z();
php > echo "$x // $x->foo // $x->foo->bar";
z // z // z->bar
Note the z->bar on the final part of the echo. The PHP parser saw $x->foo and STOPPED parsing there, leaving ->bar as plain text, and not part of the object.
You need to use the {}-extended strings syntax:
php > echo "$x // $x->foo // {$x->foo->bar}";
z // z // z
Note how ->bar didn't get output.
And ditto for a arrays:
$foo['bar']['baz'] = 'qux';
echo "$foo[bar][baz] {$foo['bar']['baz']}";
PHP Notice: Array to string conversion in php shell code on line 1
Array[baz] qux
Note how $foo['bar'] became the word Array in the first portion, while qux was properly output in the second.
Try:
<a href="<?php echo $this->item->contactlink; ?>" target='_blank'>
<?php echo $this->item->contactlink; ?>
</a>
Or shorter version:
<a href="<?= $this->item->contactlink; ?>" target='_blank'>
<?= $this->item->contactlink; ?>
</a>
Target='new' may be part of the issue - I believe target=_blank is standard. Not 100% on the issue, without seeing how you are building '$this'. Also pulling the php elements away from the html helps clear the link reference. Maybe there is an extra quotation for the link?

remove part of the new url php from link post

I would like to be able to click on a link that has specific information within it -
<a href="index.php?content=quiz-demo-1id=series-99">
id=series-99 is the information that I would like to be able to use as a variable on page - index.php?content=quiz-demo-1
1-page#1 - mysite.com/Page_1
2- Link on mysite.com/Page_1: click here - <a href="index.php?content=quiz-demo-1id=series-99">
3-new page url - mysite.com/index.php?content=quiz-demo-1
4 - new page code:
<?php
$quizname = $_GET['id'];
?>
<h2><?php echo $quizname?></h2>
I want the: id=series-99 to drop off of the new url.
Thanks!
You could use a regular expression on this (I have corrected your url and added an ampersand):
$link = "<a href='index.php?content=quiz-demo-1&id=series-99'>";
$regex = '/(id=[^&"\']+)/i'; // capture everything except the characters in the brackets
$link = preg_replace($regex, "", $link);
echo $link;
However, it would be probably safer to use the mentionned parse_url() if you are not familiar with regular expressions.

How to create an if statement for onclick on a attribute

I want to make an if statement that if they click a link with "Embed" link type, an onclick event will occur. This is my code, but it doesn't work.
I Have two link types:
External //This link will open in new tab
Embed // This is my problem, i want the embed link(video) to load in my page not open in new page or new tab that is why i need the onclick to work if they click this link type.
<a target="_blank' href="<?php get_option('url') ?>/wpwm-redirect?link_id=<?php $t_link->linkID ?>" <?php if($t_link->link_type == 'Embed') echo ' onclick="ayeLoadVideo('/wpwm-redirect?link_id=<?php $t_link->linkID ?>'); return false;"'; ?>> <?php $t_link->link_title ?> </a>
Change:
echo ' onclick="ayeLoadVideo('/wpwm-redirect?link_id=<?=$t_link->linkID ?>'); return false;"'
to:
echo ' onclick="ayeLoadVideo(\'/wpwm-redirect?link_id=' . $t_link->linkID . '\'); return false;"'
You need to escape the quotes that are inside the string, otherwise they'll end the string.
You can't use <?= inside a string. <? is used for switching from HTML code into PHP code, but you're already in PHP code when you execute echo. Use string concatenation in PHP code.

How to display special html characters in title tag

I have a anchor tag that links to a php page, I would like to use special characters in the url parameter title
The string I would like to pass in the url:
Which is better approach to use href="#" or href="javascript:void(0)"
My anchor tag href will look like this:
mypage.php?title=Which is better approach to use href="#" or href="javascript:void(0)"
on my php page, when i echo $_GET["title"]; I only get part of the string:
Which is better approach to use href="
how to display exact title which i used in my anchor tag
You have to enconde the string before sending it to the title variable.
Example:
$title = 'Which is better approach to use href="#" or href="javascript:void(0)"'
echo '<a href="mypage.php?title='. urlencode($title) . '">';
// and to get the title
echo urldecode($_GET["title"]);
Use urlencode() to output your links and urldecode() to echo them in the other page:
First page:
<?php
$link = urlencode('Which is better approach to use href="#" or href="javascript:void(0)"');
echo 'a link';
?>
And on mypage.php you'll do:
<?php
echo urldecode( $_GET['title'] );
?>
The PHP $_GET['title'] is the variable "title" of the get array which are in fact the variables after the ? in an url.
test
this would show TheTitle
But you'll probably want to url escape the title.
Link

How to use variable defined in one php tag in another php tag?

I wrote a php page which has two php tags and one script tag inside it .
<?php
$value = $_GET['hash'];
?>
<script>
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
echo $cmd;}
?>
}
</script>
I want to use $value inside another php tag ( like above it has the file I want to open ), but I am not able to do it.Is the scope of variable limited to one php tag ? if yes how can I solve this problem Please help
Your code works perfectly. The variables in one PHP tag is accessible from all other tags, unless you define them inside a PHP function.
The reason you are not seeing the echo on the screen is because the echo prints to the Javascript function.
If you view the source of the generated page, the file contents will be there.
Try this:
function execute(){
<?php
$readfile = file($value);
for ($k=0;$k<=count($readfile)-1;$k++){
$cmd = $readfile[$k];
?>
alert( <?php echo $cmd; ?> );
<?php
}
?>
}
execute();
if $value is a get then you don't need to access it as a file, it should just be a short string.
just above line 7 (the one with $readfile = file...
type:
echo "alert(The hash value is: ".$value.")";
This will make an alert display (as it is in a script tag)
p.s you should have in your opening tag

Categories