How to add post title in link - php

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

Related

Cannot pass variable with apostrophe in "a href" link

I select a list of names from mysqli database then display row details in display.php with if (isset($_GET['name']));
The link is
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=$str'>$str</a></td></tr>";
This executes correctly unless name contains '(apostrophe).
For instance $str (as input/click) shows as L'ECLIPSE but the <a> link only L'
The result in display.php is 'No data found for your request'
I have found exact same queries on this site but none of the answers have resolved my problem. Perhaps I am not implementing correctly.
I assume this is about escaping. But I know little about it.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=".urlencode($str)."'>$str</a></td></tr>";
urlencode() the string first. So you don't get this kind of problems.
Try this code.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?
name=".htmlspecialchars($str)."'>$str</a></td></tr>";
?>
Your Single quote becomes &#039 ;
I hope it will help

Get html link from database with PHP

I have a database table that contains these rows, "id" ,"link" and "name"
with link being :
<a href=\"https://www.sample.com/file1.php\">[1-200]< /a> <a href=\"https://www.sample.com/file2.php\">[201-224]< /a>
and name :
item1
I have the following PHP code to get the info from the database and return it to visitors. My problem is that the link for file2.php, is not only applied as hyper link for [201-224], the hyperlink is applied for the rest of the page content also. How do I prevent this? And thanks in advance.
echo "</br> ".$name= $row['name'];
echo "</br> ".$Torrentlink= preg_replace('/\\\\/', '',$row['Torrentlink']);
echo "</br> ";
echo "</br> ";echo "</br> ";
echo "the rest of my text goes here ";
This is a terrible way to handle this type of data. If you know they are all links then you should only be storing the link and the name (of course id and other meta data could be useful). Your current situation allows for too many errors and a maintenance problem for those working behind you. If you do not want to create a record for each link, consider storing them as JSON or some other format.
Example: (Store JSON in DB as VARCHAR)
<?php
//Populate variable from DB
//$TorrentLinks = $row{'Torrentlink'};
$TorrentLinks = '[
{"url":"https://www.sample.com/file1.php","text":"[1-200]"},
{"url":"https://www.sample.com/file2.php","text":"[201-224]"}
]';
//Convert to array
$jsonLinks = json_decode($TorrentLinks,true);
//Iterate and print links
foreach ($jsonLinks as $key => $value) {
echo "{$value["text"]}<br />";
}
Results:
[1-200]
[201-224]
Depending on how you capture the data, you could also use something like htmlspecialchars() to keep the special characters from executing.
I think there's a problem with your preg_replace('/\\\\/', '',$row['Torrentlink']);
/\\\\/ finds text with double backslash \\. It seems that your intention is to find only single backslashes to get rid of them in your links.
So try replacing it with
preg_replace('/\\/', '',$row['Torrentlink']);
For example https://regexr.com/ is a good place to check your regular expressions.
the error was simply the input html text.
the problem was the " < /a> " in the line:
<a href=\"https://www.sample.com/file1.php\">[1-200]< /a> <a href=\"https://www.sample.com/file2.php\">[201-224]< /a>
I had a space before the the backslash.

Not able to access appended variable in URL in PHP

I am trying to pass a variable from one page to another by appending it to the URL like this-
<a href='single.php?name=".$m['Title']." ' class='movie-beta__link'>
and in the next page(single.php), I am getting the url as-
$title= $_GET['name'];
echo $title;
But nothing is displayed and my URL looks like this-
http://localhost/mdb/single.php?name=
Can someone please point out if I am missing something here.
Try this
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
You're getting your single and double quotes mixed up. With single quotes you have to append variables and with double quotes you can put them inside. But when you're outside of <?php tags you can use <?= to echo a variable.
<a href="single.php?name=<?= $m['Title'] ?>" class="movie-beta__line">
This will work above:
<a href='single.php?name=<?php echo $m['Title']; ?>' class='movie-beta__link'>Title</a>
if all this is in php echo, use <a href='single.php?name='".$m['Title']."' class='movie-beta__link'>Title</a>
And its good to avoid underscores on you class names

php and echoing out string with html tags. string space truncate string and also not working as a replacement

MySql: I have my products table set up as follow:
pg_id | pg_name
1 | Pizza's calzone
2 | Kids menu
Php: Echo out the html while looping through the records in the MySQL table.
<?php do { ?>
<li>
<?php echo "<a href=". "products.php?p_group=" .$row_getproductnames[ 'pg_name'] . ">"; ?>
<?php echo $row_getproductnames[ 'pg_name']; ?>
</a>
</li>
<?php } while ($row_getproductnames=mysql_fetch_assoc($getproductnames)); ?>
My hyperlink: The link to the products.php page should look like this for records with white space in it. This post and reference the product names correctly in the products page.
http://127.0.0.1/products.php?p_group=Pizza's calzone
But it truncates after the white space to
http://127.0.0.1/products.php?p_group=Pizza's
I have checked numerous samples like using in the place of the white space, Html encryption or decryption etc. Still having problem with getting the string to link correctly. Any help would be appreciated.
You need to quote the href with double quotes:
echo "<a href=\"products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "\">"
If you use single quotes or no quotes then the ' in pg_name is misunderstood by the browser.
Your not using quotes? I don't know for sure this is causing it but usually with any parsing issues quotes will fix it.
Try replacing this line:
<?php echo "<a href='products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "'>"; ?>
If you are trying to create a valid URL, you can will want to replace the spaces with a + or %20. Either will do. I also suggest removing the apostrophes:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","", $new_url ); //Remove apostrophe
Edit:
If you are needing to use the name parameter to retrieve an item from the database, you can do it by 're-replacing' the space and apostrophe characters at the other end like this:
Build the url:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","APOSTROPHE", $new_url ); //Remove apostrophe
Then at the page where you will perform the SELECT query:
$product_name = str_replace("+"," ", $product_name); //Put spaces back
$product_name = str_replace("APOSTROPHE","'", $product_name ); //put apostrophes back
There are however much easier ways to send values to other pages such as sending a POST request

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

Categories