One of the problems I have is that my title contains an "&" but it displays as "&" when I click share.
Another problem I have is that the code below does not recognize "< br >" or "\n".
How do I make it so it will recognize "&, ', -, etc.." as well as line breaks.
<?php
$gettitle = get_the_title();
$getcontent = get_the_content();
$content = substr($getcontent, 0, 20);
$content .= "<br>".$moretexthere;
$link = get_permalink();
$title=urlencode($gettitle);
$url=urlencode($link);
$summary=urlencode($content);
?>
<a onClick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php echo $title;?>&p[summary]=<?php echo $summary;?>&p[url]=<?php echo $url; ?>','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)">Share</a>
Well, i use this as sharer url..
https://www.facebook.com/sharer/sharer.php?u=URLENCODEDURL
Use the urlencode PHP function.
"This function is convenient when encoding a string to be used in a
query part of a URL..."
http://php.net/urlencode
WordPress Example:
<?php echo urlencode(get_the_title()); ?>
Related
I am just trying to write a simple page, where I create a clickable link for HTML.
I get the first part okay, but this part yields a blank string:
$URLString = "<";
$URLString .= "/a";
$URLString .= chr(62); // that is, ">"
echo "URLString = ";
echo $URLString; // shows blank space
Any idea how to get PHP to accept this as a string, and not a command?
Thanks for help!
If I understood you well, and I think I have, you want to use:
$URLString = "<"; //that is "<"
$URLString .= "/a";
$URLString .= ">"; // that is, ">"
echo "URLString = ";
echo $URLString; // shows blank space
This are characters that represend < and > for html. Which is what htmlentities internaly does and you can find that in PHP docs
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
?>
Here is a link http://php.net/manual/en/function.htmlentities.php
You should use htmlentities():
$URLString = "<";
$URLString .= "/a";
$URLString .= chr(62); // that is, ">"
echo "URLString = ";
echo htmlentities($URLString);
Replace < by < and > by >
It sounds to me like you would like to display a link on your page if I were you I would close out of the php and just write the html. For example
<?php
//Some actual php code would go here
?>
Look where this takes you!
If you just close out of the php the php parser will just output whatever text is there. You can even generate some dynamic stuff in your php code and easily output it using the <?= ?> tag in php. Like this:
<?php
$tagText = 'Look where this takes you!';
$tagHref = 'www.google.com';
?>
<?= $tagText ?>
Both of these blocks of code produce the same output.
Also like everyone else is saying in the comments, you wont be able to see just an <a> being sent to the browser. The browser doesn't display tags, it usually displays what is between an open and closing tag.
Check out http://www.w3schools.com/ for more info on all of this stuff and some great tutorials.
This is the code I am working with:
<?php
$rss = new DOMDocument();
$rss->load('http://hugeriver.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('encoded')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = count($feed);
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<h2><a name="test">'.$title.'</a><span class="line"></span></h2>';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
I am stuck specifically with this line. I am trying to make it so the title is both the name of the ancor and the link (so when clicked it scrolls to the top). This is what I tried that doesn't work. Can anyone please show me what is wrong with my syntax?
echo '<h2><a name="'.$title'" href="#'.$title'">'.$title.'</a><span class="line"></span></h2>';
You're currently creating a link that targets itself.
If you want the link to go to the top of the document when clicked, simply link to '#':
<?php echo $title ?>
Also, name is deprecated on <a/> elements in HTML 5. Use id instead:
<?php echo $title ?>
Andre answer should solve your problem, if what you want to do is simply go to the top of the page. however if you want to go to a specific section that has id set to the value of $title then you can try this...
<?php echo $title ?>
This way when the link is clicked it will jump to the exact element with ID equal to title(which may or may not be at the top). I believe this is what you want to achieve.
<a href="#" ID="<?php echo htmlentities($title, ENT_QUOTES); ?>">
<?php echo htmlentities($title, ENT_NOQUOTES); ?></a>
Why is everyone forgetting htmlentities(), especially for attributes?
And why use $title as #target? When the $title is a variable with spaces and punctuation unfit for #target practice... Why not use an md5($title) since your generating the page dynamically? Like:
<a href="#" ID="<?php echo htmlentities(md5($title), ENT_QUOTES); ?>">
<?php echo htmlentities($title, ENT_NOQUOTES); ?></a>
and later on linking to it like this:
<a href="#<?php echo htmlentities(md5($title), ENT_QUOTES); ?>">
Go to <?php echo htmlentities($title, ENT_NOQUOTES); ?>!</a>
there are two missing dots.
try this:
echo '<h2><a name="'.$title.'" href="#'.$title.'">'.$title.'</a><span class="line"></span></h2>';
try this syntax
echo "<h2><a name=\"$title\" href=\"#$title\">$title</a><span class=\"line\"></span></h2>";
or
echo "<h2><a name='$title' href='#$title'>$title</a><span class='line'></span></h2>";
This is much cleaner and has less probability of missing a dot or closing/opening quote.
echo '<h2><a name="'.$title.'" href="#'.$title.'">'.$title.'</a><span class="line"></span></h2>';
You are missing dots after $title
Also, your links link to themself. You need to define a seperate anchor and then link to it.
Here is a code sample of PHP function which prints HTML links. For some reason there is problem with the title attributte of the a tag(' games' isn't concatenated). For example if I have $gameCategorie = '3D' I get <a title='3D'>3D games</a> I want to get <a title='3D games'>3D games</a>
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title='.$gameContent.'>';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}
Ideas about improving the quality of the code and tutorials about HTML generation by PHP are also appreciated.
All valid xhtml should have attributes enclosed in speachmarks. Try this
foreach($gamesCategories as $gamesCategorie){
$gameContent = $gamesCategorie.' games';
echo '<li><a title="'.$gameContent.'">';
echo $gameContent;
echo '</a></li>'. PHP_EOL;
}
what I want to do is want to save php variable in database (suppose {$baseUrl} ) and I am getting the data on php page with echo command and on the same page I have defined $baseUrl='/public'. I want to get the value for the base url but I'm getting simply {$baseUrl} not '/public'
in db I have <img src="{$baseUrl}/img.jpg" />
on page I have
$baseUrl = "/public";
echo $content
it is giving <img src="{$baseUrl}/img.jpg /">
how can I get <img src="/public/img.jpg" />
Of course. Strings are strings, not PHP code. You'll have to replace it yourself, e.g.:
<?php
$baseUrl = '/public';
$string = '<img src="{$baseUrl}/img.jpg" />';
$replacements = array(
'{$baseUrl}' => $baseUrl,
);
echo strtr($string, $replacements);
I think what you want to do is a string replace. The variable pointer ($baseUrl) is a string that comes from the database. If you echo it, it is still just a string. What you need to do is something like this:
<?php
echo str_replace('$baseUrl', $baseUrl, $varFromDB);
?>
Or do I understand your question wrong?
With
str_replace($content, '{$baseUrl}', $baseUrl)
.
I am guessing that you are using the wrong quotes:
echo '<img src="{$baseURL}/img.jpg" />';
rather than
echo "<img src='{$baseURL}/img.jpg'/>";
You want the $baseUrl variable to be evaluated? You have to put the variable outside of the string definition:
echo '<img src="' . $baseUrl . '/img.jpg" />';
or put it between double quotes (strings enclosed in double quotes are parsed by PHP):
echo "<img src=\"{$baseUrl}/img.jpg\" />";
Based on your comments, you need this solution:
$content = str_replace("{$baseUrl}", $baseUrl, $content);
You may want to use the eval() function...
This is not a good idea, but it would achieve what you want.
In my Wordpress site, I am trying to retrieve the most recent 10 posts and store them in a string. After that I will write this content into a text file. Below is the code I am using.
<?php $str = ''; ?>
<?php
require_once('../wp-blog-header.php');
query_posts('&showposts=10&order=DESC&caller_get_posts=1');
while (have_posts()) : the_post(); ?>
<?php $str .= '' .the_title() . ''; ?>
<?php endwhile; ?>
<?php $fp = fopen("latestposts.txt", "w");
fwrite($fp, $str);
fclose($fp);?>
The problem is, when I execute this page, the permalink and title are returning in this page and empty ''....'' tags are coming in text file. If I am not using the string, the href tags are returning correctly in the same file.
the_permalink() and the_title() does not return anything they are to print values.
You have to use their get_ version. Those are get_permalink() and get_the_title()
<?php $str .= '' .get_the_title() . ''; ?>
This is more of a wordpress question, but you should be using get_permalink() and get_the_title() instead of the functions you have there. Those functions will echo the link and title, and not return it in string form for use in your concatenation.