remove part of the new url php from link post - php

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.

Related

How to add post title in link

I am using adcenter ads, which are as follows-
ads.ad-center.com/offer?prod=101&ref=5030200&q=Keyword
Now i want to replace Keyword in link with post title automatically. i have tried this code, but it return first word from title, code is-
<?php
$title = get_the_title();
echo "<a href=http://ads.ad-center-com/offer?prod=101&ref=5030200&q=$title >Download</a>"
?>
please solve it or tell me other method to add title in link automatically.
Thanks
When you are adding some string in URL remember to encode the string.
$title = urlencode(get_the_title());
echo "<a href='http://ads.ad-center-com/offer?prod=101&ref=5030200&q=$title'>Download</a>";
In your case the value of the href attribute in your a tag must be encapsulated into double quotes, as it can contain spaces. You can escape them like this:
echo "Download";
or you can use the concatenation:
echo 'Download';

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

Add php code to end of every html link

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;?>

Extract links from mysql and make it clickable?

I have a database table that stores URL.What I need is grab those URL's from table and make it click-able with the URL's title as anchor.
This is what I have tried:
while($row4 = mysql_fetch_assoc($result4))
{
echo "".$row4['Title1']. "";
}
It displays for example my tilte1 that is youtube and Url1 is www.youtube.com.
But when I click on it it is going to localhost/mysite/www.youtube.com
How can I fix this?
try:
echo "".$row4['Title1']. "";
Add http:// in front of the link. Then it will go to where you wanted.
you need http:// in front.
echo ''.$row4['Title1']. '';
Can you check if your Url1 field is a proper url? see if it has http:// protocol in the url. if not you will need to add it to prepend it to your table or programmatically prepend http:// protocol to your link.
Additionally you can use below function taken form codeigniter framework. It prepares your link for url. do prep_url($row4[Url1]) instead of just $row4[Url1];
function prep_url($str = '')
{
if ($str == 'http://' OR $str == '')
{
return '';
}
$url = parse_url($str);
if ( ! $url OR ! isset($url['scheme']))
{
$str = 'http://'.$str;
}
return $str;
}
Try with this
while($row4 = mysql_fetch_assoc($result4))
{
echo "<a href ='http://".$row4['Url1']."'>".$row4['Title1']. "</a>";
}
You should make an absolute link from that, and don't forget to put attributes' values in quotes.
I suggest this:
echo ''.$row4['Title1']. '';
//by doing this you also won't need any of \ slashes
I enter urls enclosed in quotes, example:
"http://google.com"
Then I use:
.$row['date']."< a href=".$row['title'].">".$row['title']."< /a>".
the result is a clickable link in the form of:
http://google.com
remove the space between < and a, ( i had to add a space for the code to post.

Drupal - print current URL as link, but remove the final argument

I'm trying to add a 'back' link within page.tpl.php (replacing a proper breadcrumb for a certain content type...).
My goal is to create a link that pulls in the current URL, but removes the final argument. So a page mysite/x/y would have a link mysite/x/ (or mysite/x).
Is this possible? (The URLs are aliased).
<?php
$path = ???
$my_link = l('Back', $path);
?>
<?php if (($node->type == 'marketplace_item')): ?>
<div id="breadcrumb" class="nav"><?php print $my_link; ?></div>
<?php endif; ?>
Cheers,
James
if this the case always you can build the URL manually
$my_link = $base_url . arg(0);
or count the args then remove the last one

Categories