Add php code to end of every html link
Get the solution ↓↓↓Solution:
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.
Answer
Solution:
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;
}
Answer
Solution:
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 files
= the substitute commandold
= a regular expression describing the word to replacenew
= the text to replace it withg
= replace all occurrence*.php
= wildcard file name
Answer
Solution:
Suggestion:
Since you have to modify all your code,
why not remove the hard-coding ofhttp://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;?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: too few arguments to function laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.